Thursday, September 5, 2013

Json.Net: Using SelectToken to get a value without knowing an element name?

Json.Net: Using SelectToken to get a value without knowing an element name?

I have a simple JSON file which i need to get the value of a token, i have
tried using SelectToken but the problem is that the name of the elements
are dynamic. Here is my JSON file
{
"name": "testdata",
"items": {
"myItemName": {
"located": true
}
}
}
I need to get the value of "located" (above), If you see the element name
"myItemName", this is dynamic and not fixed. So I did try using the dot
notation of SelectToken.
I came up with something like so, but it didn't work - problem is the [0]
bool located = (bool)jsonObject.SelectToken("items.[0].located");
Within Items, there can be more than 1 dynamic element, so for example,
this is valid.
{
"name": "testdata",
"items": {
"myItemName": {
"located": true
},
"myOtherItemName": {
"located": true
}
}
}
So looking above, you can see I have "myItemName" and "myOtherItemName", I
would like to get the values of "located" in each one using somekind of
foreach
Is this at all possible ? Using SelectToken?
Thanks

No comments:

Post a Comment