简体   繁体   中英

C# parse array in json

I have following json structure:

{
    [{
        "name": "2542",
        "type": "FOLDER",
        "size": 0,
        "time": 0,
        "items": [{
            "name": "10-1432927746000.ksf",
            "type": "FILE",
            "size": 225,
            "time": 1433019520,
            "items": null,
            "info": {
                "seller": 10,
                "count": 2
            }
        }],
        "info": null
    }]
}

how can I parse it with C#? I have try var results = JsonConvert.DeserializeObject<dynamic>(json) but the result is an error:

Invalid property identifier character: [. Path '', line 1, position 1.

The JSON posted doesn't lint so I suspect this is the root of your problem.

However this does:

{
    "things":[{
        "name": "2542",
        "type": "FOLDER",
        "size": 0,
        "time": 0,
        "items": [{
            "name": "10-1432927746000.ksf",
            "type": "FILE",
            "size": 225,
            "time": 1433019520,
            "items": null,
            "info": {
                "seller": 10,
                "count": 2
            }
        }],
        "info": null
    }]
}

Note how the outermost array has now has an identifier which is required ; that is to say your parsed object will have a things property which is an array of that inner structure.

Complementing the @Stephen awswer, you can yet use only the inner array, like in this sample .

[{
        "name": "2542",
        "type": "FOLDER",
        "size": 0,
        "time": 0,
        "items": [{
            "name": "10-1432927746000.ksf",
            "type": "FILE",
            "size": 225,
            "time": 1433019520,
            "items": null,
            "info": {
                "seller": 10,
                "count": 2
            }
        }],
        "info": null
    }]

Anyway, the issue seems to be your original json realy. =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM