Reputation: 41
I have a json string that I would like to iterate through to retrieve a specific value. In this object, there are multiple items, each with the same elements. What i am trying to do is search through each item for its request_item.u_requested_for.user_name, and compare it to a different value. If it does NOT match, move on. If it does match, then i want to retrieve the request_item.u_requested_for.sys_id value. Additionally, the size of the json will not always be the same. Sometimes there will be 5 elements, other times 20. How can I loop through each item, do the comparison, and then retrieve the other value that I need?
I've played around with this using other ideas I've found, but I'm unable to really get it working. First, should i parse the string into an object using JObject.Parse()? Or should I continue working with this as as string?
I've tried using a JsonTextReader to read through each line, but I'm unsure how I would retrieve the value I need once I find the item that matches the criteria.
{
"result": [
{
"request_item.sys_id": "db6e3306dbc3ef8c791a777a8c961986",
"request_item.u_requested_for.sys_id": "8416ccb3dba9df8c1ddee3a84b961911",
"request_item.u_requested_for.user_name": "H298956"
},
{
"request_item.sys_id": "e5990f89db47ebc8bd663ebd7c96198f",
"request_item.u_requested_for.sys_id": "878ce1d5dbcd0300cc78dd0b5e961987",
"request_item.u_requested_for.user_name": "E092733"
},
{
"request_item.sys_id": "87970c08db4be388791a777a8c9619ee",
"request_item.u_requested_for.sys_id": "484a1cf5db5f83c04f6489584b961952",
"request_item.u_requested_for.user_name": "H281111"
}
]
}
In this case, I want to iterate through each item and look for the value "H281111". Once I find that value, I want to extract the "request_item.u_requested_for.sys_id value", which in this case is "484a1cf5db5f83c04f6489584b961952".
Upvotes: 1
Views: 2858
Reputation: 37059
Depending on your requirements, you may want a much quicker and dirtier solution than Michaël's. If the contents of this JSON string are used in multiple places in your application, I urge you to write the classes and deserialize properly as he suggests. You'll be glad you did.
But if you only need one value from this JSON string in one place, this will work.
var jobj = JObject.Parse(json);
var user_name = "H281111";
var sys_id = jobj["result"]
.Where(r => (String)r["request_item.u_requested_for.user_name"] == user_name)
.Select(r => (String)r["request_item.u_requested_for.sys_id"])
.FirstOrDefault();
Elphas Tori suggests the following improvement using the ?[]
(null-conditional element access) operator:
var sys_id2 = (string)jobj["result"]
.FirstOrDefault(r =>(string)r["request_item.u_requested_for.user_name"] == user_name)
?["request_item.u_requested_for.sys_id"];
If that LINQ code looks too much like gibberish to you, you can use a more conventional loop approach:
foreach (var r in jobj["result"])
{
// etc.
}
Upvotes: 4
Reputation: 171
You need to deserialize this into POCO objects to iterate them. Here you can find an example how to implement it.
Upvotes: 2
Reputation: 443
I think you should have a class representing the objects in this Json file, deserialize the Json file into your class and then use LINQ to find what you need in your list of objects.
Result would be an array of requestItem
Request item would have the following fields :
Requested for would have the following fields :
You can even be fancy and use a base class that has sys_id in it to save some time.
Upvotes: 2