I have the following string variable ‘myJsonString’, which is a JSON-array as a string containing the following:
[
]
I want to extract the value for id:myId3 = “iWantThisValueAsAString”.
I’ve tried the following:
JObject myJsonObject = JObject.Parse(myJsonString);
string theValueAsAString= myJsonObject[“myId3”].ToString();
but that doesn’t work. I am not interested in creating a model class for it – if that is avoidable.
What am I doing wrong?
Thanks in advance.
https://www.newtonsoft.com/json/help/html/QueryJsonLinq.htm
https://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm
https://www.newtonsoft.com/json/help/html/QueryJson.htm
you may try to use JArray a = JArray.Parse(json);
then iterate over a and get id
property for array element
It doesn’t work, because myJsonObject is an array, a sequence of JObjects
.
You are thinking that it is a hash set, but for that to happen the top level would need to be not an array, but an object with properties like this:
However, what you have is an array:
As an array, the only way to get the item you want is to loop over each and compare the current objects id
, though I’m sure that can be done with LINQ or something.
You can cast myJsonObject to a JArray.
You (and the others) are absolutely right – I fixed it. Thanks.