0

I know this question has been ask many times but I have tried many solutions and nothing has work so far I have tried using Json.Net, JavaScriptSerializer...etc..

Here is a sample of Json I want to deserialized. Note I am building this in a Win Form app using C# I want to be able to deserialize into a Class.

{
  "data": [
    {
        "ID": "4f351ef7005fc2b7a6fcd3e4a4ddb4fe",
        "name": "Distorted Packages",
        "objCode": "OPTASK",
        "owner": {
            "ID": "4f16e4cb0043332b4bfc602927dbb1c0",
            "name": "Marjorie Rolan",
            "objCode": "USER",
            "title": null,
            "phoneNumber": null,
            "lastSurveyedOn": null,
            "showWhatsNew": true
        },
        "primaryAssignment": null
    },
    {
        "ID": "4f3522a40060146c9fd7f36e49a4cf47",
        "name": "More issues on k.com",
        "objCode": "OPTASK",
        "owner": {
            "ID": "4f16e4cb0043332b4bfc602927dbb1c0",
            "name": "Marjorie Rolan",
            "objCode": "USER",
            "title": null,
            "phoneNumber": null,
            "lastSurveyedOn": null,
            "showWhatsNew": true
        },
        "primaryAssignment": null
    }
  ]
}

Thanks.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
lanhhhuyet
  • 23
  • 2
  • 1
    What is the error you're getting? – TGH Oct 09 '13 at 00:46
  • possible duplicate of [Deserialize JSON into C# dynamic object?](http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – pickypg Oct 09 '13 at 00:47
  • First order of business when answering a JSON question: run the sample JSON through [jsonlint](http://jsonlint.org). Your JSON sample fails validation, so it's likely every JSON parser will fail. –  Oct 09 '13 at 00:50
  • @MikeW, it is actually Valid JSON. – NomadTraveler Oct 09 '13 at 00:52
  • 1
    I just did (and originally it wasn't valid JSON but I assumed it was a copy/paste error because it was missing a closing `]` on the data array and closing `}` around the whole thing. – Jason Sperske Oct 09 '13 at 00:52
  • Well, there you go. Now, the OP has Valid JSON to parse. – NomadTraveler Oct 09 '13 at 00:54
  • @Somya - it is valid JSON now: it's been edited since I validated it. –  Oct 09 '13 at 00:54
  • @JasonSperske - you may be correct: It might just be a cut/paste slip. Of course, if it's not, and the OP posted the actual JSON he's getting then you've masked the cause. I think we need the OP to confirm that your edit is valid. –  Oct 09 '13 at 00:56
  • Sorry guys.. I missed the ]}. Thanks @JasonSperske – lanhhhuyet Oct 09 '13 at 01:05
  • I also assumed it was a mistake because of the trailing comma (I figured there was a lot of JSON and the OP didn't want to dump a lot of text in the question) – Jason Sperske Oct 09 '13 at 01:07
  • Yes!, exactly @JasonSperske – lanhhhuyet Oct 09 '13 at 01:11

1 Answers1

1

So following the JSON.net path you need a c# class that maps to your data. For the posted JSON this should work:

class JSONThing {
  public JSONThingData[] { get; set; }
}

class JSONThingData {
  public string ID { get; set; }
  public string name { get; set; }
  public string objcode { get; set; }
  public JSONThingDataOwner owner { get; set;}
}
...

To deserialize this all you have to do is pass the root class and the JSON string to the JsonConvert.DeserializeObject function like this:

JSONThing thing = JsonConvert.DeserializeObject<JSONThing>(json);
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124