string json = "{\"People\":[{\"FirstName\":\"Hans\",\"LastName\":\"Olo\"}
{\"FirstName\":\"Jimmy\",\"LastName\":\"Crackedcorn\"}]}";
var obj = JObject.Parse(json);
List<string> first;
List<string> last;
foreach (var child in obj["People"].Children())
{
var name = child.First()["countryName"].ToString();
var two = child.First()["countryCode"].ToString();
var three = child.First()["isoAlpha3"].ToString();
countries.Add(name);
twoCharCodes.Add(two);
threeCharCodes.Add(three);
Console.Write("Name:\t\t{0}\n2CharCode:\t{1}\n3CharCode:\t{2}\n\n", name, two, three);
}
我希望找到一种方法,将每个FirstName值添加到第一个列表中,并将LastName值添加到最后一个列表中。如何最好地实现这一点?
以上代码在以下位置出现错误:
var name = child.First()["countryName"].ToString();
出现如下错误:
Cannot access child value on Newtonsoft.Json.Linq.JProperty
有什么建议吗?
countryName
和其他信息?我只看到People
下有FirstName
和LastName
。请帮忙确认一下。 - Jonesopolis