Click here to Skip to main content
14,974,887 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Could any one point me in the direction on how to deserialize the json.

{
   "UpdatePack":"updatePacks\/1585654836.pack",
   "Updates":[
      {
         "Name":"MsgBoxEx",
         "version":"1.5.14.88",
         "ChangeLog":"BugFix: Form didn't resize correct.",
         "Hash":"5FB23ED83693A6D3147A0485CD13288315F77D3D37AAC0697E70B8F8C9AA0BB8"
      
},
      {
         "Name":"Utilities",
         "version":"2.5.1.58",
         "ChangeLog":"StringManagement updated.",
         "Hash":"05E6B3F521225C604662916F50A701E9783E13776DE4FCA27BE4B69705491AC5"
      
}
   
]
}




I'm all new to json.

What I have tried:

Read what I could find of articles, tried with a class to list.
Posted
Updated 31-Mar-20 3:34am
v2

You can read my article to understand how to work with JSON data in C#, From zero to hero in JSON with C#[^]. It gives you an overview of JSON data, Newtonsoft library for JSON handling and a bunch of other tips. Now as for the question and this JSON data, read the rest of the answer.

You can create a Dictionary<int, Type> and store the values in that collection. The reason is that in C#, 0 and 1 cannot be used as names for properties. You will need to create a class that will catch the objects in Dictionary. This would give you a start:

C#
public class MyData {
   public string Name { get; set; }
   public string Version { get; set; }
   public string ChangeLog { get; set; }
   public string Hash { get; set; }
}
Now your JSON object holder will become:

C#
public class JsonObjectHolder {
   public string Updates { get; set; }
   public Dictionary<int, MyData> { get; set; }
}
You can now deserialize the JSON to objects using Json.NET:
C#
var jsonObjectHolder = JsonConvert.DeserializeObject<JsonObjectHolder>(yourData);
It would be good to keep the names consistent so that library can help you out. But if you cannot control the data from API or the naming of the JSON objects, then you can use this approach to deserialize the values.

This is a valid JSON because JavaScript doesn't pretty much enforce anything on the naming conventions. In JavaScript, the following two statements are okay:
C#
var firstObject = jsonObjectHandler.prop;
var firstObject = jsonObjectHandler['prop'];
Thus, your code is valid in JavaScript realm and you can access the values as:
C#
var firstObject = jsonObjectHandler['0'];
In C#, however, you cannot do this for properties and you need to use a Dictionary.
   
Comments
Lupu5R3x 30-Mar-20 7:45am
   
Hi Afzaal
I got an error using this: public class JsonObjectHolder {
public string Updates { get; set; }
public Dictionary<int, mydata=""> { get; set; }
}
But fixed it by adding a name to the Dictionary prop, public Dictionary<int, mydata=""> Dict { get; set; }

But I cant get in contact with the dictionary, I tried:
var jsonTest = JsonConvert.DeserializeObject<jsonobjectholder>(serverResponseJsonString.Result);

Console.WriteLine(jsonTest.UpdatePack);
Console.WriteLine(jsonTest.Dict.Count);

foreach (KeyValuePair<int, updateslist=""> item in jsonTest.Dict)
{
Console.WriteLine(item.Key);
}
But I get an Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at " Console.WriteLine(jsonTest.Dict.Count);", and I out comment that and tries my foreach loop I get the same error.

What Am i doing wrong?

/LR
Lupu5R3x 31-Mar-20 9:33am
   
Afzaal's answer pointed me in the right direction, though it needed some more to work, but sins I did paste the wrong Json, my guess is that Afzaal would had thrown the right answer, so I accept his solution.
I have added the code that works below.
This code works for me to access the Json.

The classes:
public class JsonObjectHolder
   {
       public string UpdatePack { get; set; }
       //public Dictionary<int, MyData> { get; set; }
       public Dictionary<int, UpdatesList> Updates { get; set; }
   }
   public class UpdatesList
   {
       public string Name { get; set; }
       public string Version { get; set; }
       public string ChangeLog { get; set; }
       public string Hash { get; set; }
   }

And the Deserialiazing:
var json = JObject.Parse(serverResponseJsonString.Result);
            var list = json["Updates"]?.ToObject<List<UpdatesList>>();
            //var dict = new Dictionary<int, UpdatesList>();
            var holder = new JsonObjectHolder
            {
                UpdatePack = json["UpdatePack"]?.Value<string>(),
                Updates = list.Select((updatesList, index) => new { updatesList, index })
                                .ToDictionary(x => x.index, x => x.updatesList)
            };
   
Comments
Richard Deeming 1-Apr-20 13:37pm
   
Change your JsonObjectHolder class to:
public class JsonObjectHolder
{
   public string UpdatePack { get; set; }
   public List<UpdatesList> Updates { get; set; }
}
Then you can deserialize with Afzaal's code:
var holder = JsonConvert.DeserializeObject<JsonObjectHolder>(yourData);
Lupu5R3x 2-Apr-20 2:17am
   
That's what I did, some one told me that I could past my json as C# classes, and that save almost the same result as Afzaal's.

/LR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900