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:
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:
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:
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:
var firstObject = jsonObjectHandler.prop;
var firstObject = jsonObjectHandler['prop'];
Thus, your code is valid in JavaScript realm and you can access the values as:
var firstObject = jsonObjectHandler['0'];
In C#, however, you cannot do this for properties and you need to use a
Dictionary
.