I have a JSON string below:
{
"data": [
{
//data for first
},
{
//data for second
},
{
//data for third
},
]
}
From this JSON string, I would like to retrieve the number of objects within the "data" element, which in this case, would be 3.
What would be the best approach to accomplish this? I used the .Length property, but that returned a number that I did not expect.
I am using C#.
.Lengthwill give you the number of charachter in theString. To find the objects you will need to deserialise the JSON usingJavaScriptSerializerorDataContractJsonSerializer, both of which will require a compatible type. Alternatively you could use one of many third party assemblies or, roll your own deserialiser (not reccomended) or finally you could count the number of braces, accounting for opening, closing and nesting and deduce the count that way.