I have following JSON format.
{
"data": [
{
"id": 1,
"variants": [
{
"name": "Test",
"images": {
"WHITE": [
{
"id": 13,
"name": "Testname"
}
]
}
}
]
}
]
}
Now I am Desalinizing it using following method :
var items = JsonConvert.DeserializeObject<dynamic>(content);
And then in items I am getting variants.
I am using dynamic because in this images node in some record it is coming as empty array and in some it is coming as JSON object so..
I have following dynamic object. If I write down following console line then below output will come.
Console.WriteLine(variant.images)
It’s output is looking like this below :
{
"WHITE": [ // I want to get this WHITE in one variable, this color name.
{
"id": 13,
"name": "Testname"
}
]
}
Now this WHITE will come dynamic in few records it can come BLACK and in few it can come RED..
So based on that color value I need to put further logic. So how can I access that value only.
I want to save that color name value in one variable and then want to use it.
Thanks
2
Answers
I’d avoid deserializing into
dynamic
, rather useJObject.Parse()
.If you know what color name to search for:
If you don’t know what the color name is, but know where it should be:
My token selectors us a lot of
[0]
‘s, so adapt those if the object you are looking for isn’t the first object in the arrays.[*]
might work, but use with care.I have showed you several times already how to use LINQ