I have a Json object that looks like this:
{
wvw_matches: [
{
wvw_match_id: "1-4",
red_world_id: 1011,
blue_world_id: 1003,
green_world_id: 1002,
start_time: "2013-09-14T01:00:00Z",
end_time: "2013-09-21T01:00:00Z"
},
{
wvw_match_id: "1-2",
red_world_id: 1017,
blue_world_id: 1021,
green_world_id: 1009,
start_time: "2013-09-14T01:00:00Z",
end_time: "2013-09-21T01:00:00Z"
}
]
}
It contains a lot more objects in the array than the example above shows. Anyway, I need to select the Json object based on the wvw_match_id.
How would I achieve this? :)
Since it seems from the comments that you're already semi-comfortable with the idea of using JObject
and Linq, here is an example program demonstrating how to get a specific match from your JSON by ID using that approach:
class Program
{
static void Main(string[] args)
{
string json = @"
{
wvw_matches: [
{
wvw_match_id: ""1-4"",
red_world_id: 1011,
blue_world_id: 1003,
green_world_id: 1002,
start_time: ""2013-09-14T01:00:00Z"",
end_time: ""2013-09-21T01:00:00Z""
},
{
wvw_match_id: ""1-2"",
red_world_id: 1017,
blue_world_id: 1021,
green_world_id: 1009,
start_time: ""2013-09-14T01:00:00Z"",
end_time: ""2013-09-21T01:00:00Z""
}
]
}";
string matchIdToFind = "1-2";
JObject jo = JObject.Parse(json);
JObject match = jo["wvw_matches"].Values<JObject>()
.Where(m => m["wvw_match_id"].Value<string>() == matchIdToFind)
.FirstOrDefault();
if (match != null)
{
foreach (JProperty prop in match.Properties())
{
Console.WriteLine(prop.Name + ": " + prop.Value);
}
}
else
{
Console.WriteLine("match not found");
}
}
}
Output:
wvw_match_id: 1-2
red_world_id: 1017
blue_world_id: 1021
green_world_id: 1009
start_time: 9/14/2013 1:00:00 AM
end_time: 9/21/2013 1:00:00 AM
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