I am getting an array when I deserialize my JSON.
I can access the array with foreach.
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
foreach (dynamic result in obj.Results.output1.value.Values)
{
}
But I need to get size of the array to access the last element direct.
Eg.
obj.Results.output1.value.Values[size-1]
How can I do that?
I need to get for example the "Y" in "Values"
{
"Results": {
"output1": {
"type": "table",
"value": {
"ColumnNames": [
"I01",
"I02",
"I03",
"O01",
"Scored Probabilities for Class \"0\"",
"Scored Probabilities for Class \"1\"",
"Scored Probabilities for Class \"2\"",
"Scored Labels"
],
"Values": [
[
"-0.96624",
"0.02918",
"-0.44237",
null,
"3.25456957391002E-12",
"0.000107838139228988",
"2.76633869589205E-07",
"Y"
]
]
}
}
}
}
If I print I get this JSON
Console.WriteLine(obj.Results.output1.value.Values);
[
[
"-0.96624",
"0.02918",
"-0.44237",
null,
"3.25456957391002E-12",
"0.000107838139228988",
"2.76633869589205E-07",
"Y"
]
]
And the Count prints 1
Console.WriteLine(obj.Results.output1.value.Values.Count);
I almost there, I need the last element o the size to access by index in the inside array.
I could get the last element with:
Console.WriteLine(obj.Results.output1.value.Values[0].Last);
And the array size with:
Console.WriteLine(obj.Results.output1.value.Values[0].Count);
The JSON shows that Results is an object, not an array or list. What you're iterating is the properties. It's not a collection. It will be a JObject, which exposes .Last.
See here for more information
You simply need to write:
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var lastProperty = obj.Last;
Might be useful to cast to a JObject (or a Dictionary<string, object> as well to help with future issues.
JObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
Accessing Values:
dynamic thing = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var t = thing.Results.output1.value.Values.Count;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With