My jsonarray has a serialized List of Products with Id and Name property.
JArray jsonarray = JArray.Parse(json);
var name = // Get value for Name property which has Id 1.
How can I do this?
You can try using linq:
JArray jsonarray = JArray.Parse("[{'Id':3, 'Name': 'Product3'}, {'Id':1, 'Name': 'Product1'}, {'Id':2, 'Name': 'Product2'}]");
var name = jsonarray
.FirstOrDefault(x => x.Value<int>("Id") == 1)
.Value<string>("Name");
Note that you should perform null check because FirstOrDefault
may return null if an element with property Id == 1
is not found.
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