【发布时间】:2015-06-02 12:07:55
【问题描述】:
我在 C# 中工作,在那里我创建了一个小项目。在项目中我正在解析一个 json 文件。我只是想问一下我可以解析或使用 json 文件吗.....
JSON 文件
{
"Nodes": [
{
"Number of Location":"4",
},
{
"longitude": "22.3 ",
"latitude": "45.3",
"Demand": "5"
},
{
"longitude": "26.3 ",
"latitude": "46.3",
"Demand": "6"
},
{
"longitude": "35.1 ",
"latitude": "48.2",
"Demand": "2"
}
]
}
C# 代码
string path = null;
Console.WriteLine("enter the path where order matrix is loacated");
path = Console.ReadLine();
System.IO.StreamReader myFile = new System.IO.StreamReader(path);
string myString = myFile.ReadToEnd();
JObject o = JObject.Parse(myString);
Console.WriteLine(o["Nodes"][0]["Number of Location"]);
for (int i = 0; i < 5; i++)
{
Console.WriteLine(o["Nodes"][i+1]["longitude"]);
Console.WriteLine(o["Nodes"][i+1]["latitude"]);
Console.WriteLine(o["Nodes"][i+1]["Demand"]);
}
当我解析 JSON 文件时,我没有得到 Number of location 的值。所以你能帮我看看如何获取 nnumber of location 的值。强>。
【问题讨论】:
-
您是否获得了
null值? -
不,实际上当我从文件中获取字符串时,该字符串不包含“位置编号”字段,但所有重命名字段都在获取。
-
该字段包含或打印出什么?可以发在这里吗?
-
我相信是因为“位置数量”中的空格
-
json无效。Array似乎无效。