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);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法将每个FirstName值添加到第一个List中,并与LastName值和最后一个List相同.这样做的最佳方法是什么?
上面的代码打破了:
var name = child.First()["countryName"].ToString();
Run Code Online (Sandbox Code Playgroud)
有这个错误:
Cannot access child value on Newtonsoft.Json.Linq.JProperty
Run Code Online (Sandbox Code Playgroud)
有什么建议?