I'm not sure where you're getting the type JsonArray
, but JSON.net uses JArray
to represent a JSON array.
To access an item in the array, just use an indexer with the index of the item you want to get.
Then for each object, you can pretty much treat them as a dictionary and access members by name through the indexer.
Do note however that when you access anything through the indexer, you need to cast the value to the appropriate type. Accessing members will have the type JToken
.
var json = @"[
{ ""Id"":0, ""AccountId"":""7.0"", ""DeptName"":""Dept7"" },
{ ""Id"":1, ""AccountId"":""8.0"", ""DeptName"":""Dept8"" },
{ ""Id"":2, ""AccountId"":""9.0"", ""DeptName"":""Dept9"" },
{ ""Id"":3, ""AccountId"":""10.0"", ""DeptName"":""Dept7"" },
{ ""Id"":4, ""AccountId"":""11.0"", ""DeptName"":""Dept7"" },
{ ""Id"":5, ""AccountId"":""12.0"", ""DeptName"":""Dept8"" }
]";
var arr = JsonConvert.DeserializeObject<JArray>(json);
// iterate through all the objects
foreach (JObject obj in arr)
{
var id = (string)obj["Id"];
var accountId = (double)obj["AccountId"];
var departmentName = (string)obj["DeptName"];
// do stuff
}
var second = (JObject)arr[1]; // or directly access the second object
If you had targeted .NET 4, you could use dynamic
to make things even easier.
dynamic arr = JsonConvert.DeserializeObject(json);
foreach (dynamic obj in arr)
{
string id = obj.Id;
double accountId = obj.AccountId;
string departmentName = obj.DeptName;
// do stuff
}