我正在解析 JSON 以提取名称详细信息。但是,我遇到了一个错误。 你能帮我提取姓名的详细信息吗?
var jsonResult = JObject.Parse(jsonFileContents);
Console.WriteLine("Name :" + jsonResult["Name"]);
json文件内容:
[
{
"Name": "Joe",
"Age": 25,
"Rank": 1
},
{
"Name": "Peter",
"Age": 32,
"Rank": 2
}
]
错误: 未处理的异常:Newtonsoft.Json.JsonReaderException:从 JsonReader 读取 JObject 时出错。当前 JsonReader 项目
最佳答案
看起来,你输入的 JSON 是 JSON Array。需要解析如下
需要指定Index来提取各个元素。例如,提取名字。
var jsonResult = JArray.Parse(jsonFileContents);
Console.WriteLine("Name :" + jsonResult[0]["Name"]);
如果你想提取所有的名字细节,那么你可以像下面这样迭代获取
从 JSON 数组中提取所有名称详细信息:
var jsonResult = JArray.Parse(jsonFileContents);
for(int i = 0; i < jsonResult.Count; i++)
{
Console.WriteLine(jsonResult[i]["Name"]);
}
关于c# - 多次存在相同名称时无法解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50608085/