3

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.

Pejman
  • 2,442
  • 4
  • 34
  • 62

2 Answers2

3

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.

Stephen Byrne
  • 7,400
  • 1
  • 31
  • 51
3

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. =)

Daniel Oliveira
  • 1,101
  • 9
  • 26