0

How can I deseralize this JSON to C# collection with Json.NET ?

{
    "3396": [{
        "id": 767570,
        "t": {
            "0-43": [{
                "id": 71968108,
                "n": "No",
                "v": 1.55,
                "bt": 1
            }, {
f0rza
  • 480
  • 3
  • 17

3 Answers3

0

Create a model in C#:

public class YourModelJSON
{
    [JsonProperty("yourmodel")]
    public YourModel YourModel { get; set; }
}

public class YourModel
{
    [JsonProperty("id")]
    public string myId{ get; set; }

    [JsonProperty("t")]
    public string myT { get; set; }

    ...
}

And then deserialize your json with:

JsonConvert.DeserializeObject<List<YourModelJson>>(json);
Joakim M
  • 1,793
  • 2
  • 14
  • 29
0

I think you will have a hard time putting this json into classes, so I'd go for the dynamic solution here:

dynamic result = JsonConvert.DeserializeObject(json); // json -> escaped string
var array3396 = result["3396"];
var arrayT = array3396[0]["t"];
var array043 = arrayT["0-43"][0];
var id = array043["id"]; // output: 71968108
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
  • Thanks, I was looking for strong type deserialization. It's easy to create classes with special paste : http://stackoverflow.com/questions/18526659/how-to-show-the-paste-json-class-in-visual-studio-2012-when-clicking-on-paste – f0rza Jun 02 '15 at 10:55
  • i know... have you tried special paste and did it work? – stefankmitph Jun 02 '15 at 10:57
  • I did, just removed from fiddle 'roottoobject' parent class made by VS output. – f0rza Jun 02 '15 at 11:01
0

Made it as follows:

                dynamic data = h.GetDynamicJSON(urlDet);
                if (data != null)
                {
                    foreach (var d in data)
                    {
                        var json = JsonConvert.SerializeObject(d.Value);
                        var obj = JsonConvert.DeserializeObject<List<Details>>(json);

                    }
                }

Got source as dynamic object, then serialized 'dynamic named' value, and deserialized it to strong type object.

f0rza
  • 480
  • 3
  • 17