我有一个 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"
}
]
}
它在数组中包含的对象比上面的示例显示的要多得多。无论如何,我需要根据 wvw_match_id 选择 Json 对象。
我将如何实现这一目标? :)
因为从评论看来,您已经对使用的想法感到半满意JObject
和 Linq,下面是一个示例程序,演示如何使用该方法通过 ID 从 JSON 中获取特定匹配项:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)