我在将JSON数字列表读入c# int[]数组时遇到了困难。
我尝试了SO上的几个建议,但都没有成功。 我该如何使用JSON.net进行操作?
从JSON文件中提取:
{
"course": "Norsk",
"grades": [6, 3, 5, 6, 2, 8]
}
我在c#中尝试过的:
// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);
// Parsing the information to a format json.net can work with
JObject data = JObject.Parse(json);
JToken jToken = data.GetValue("grades");
jGrades = jToken.Values<int>().ToArray();
同时:
// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);
// Parsing the information to a format json.net can work with
JObject data = JObject.Parse(json);
for (int o = 0; o < 6; o++) {
var grades = from p in data["Info"[i]] select (int)p["grades"[o]];
jGrades.Add(Convert.ToInt32(grades));
}
如您所见,从C#提取的代码中,我已经尝试使用数组和列表,但无法使其正常工作。
在第一个例子(使用数组)中,我得到了一个“System.NullRefrenceException”错误;而在使用列表的例子中,我遇到了多个错误,例如“无法将类型为'whereselectlistiterator'2 [Newtonsoft.JSON]的对象转换为'type 'system.iconvertible''”等。
如果您有任何帮助或提示,将不胜感激。
var
信息呢?因为它不是一个数组,我能否对其进行索引并将其添加到列表中?例如:var list
...grades.Add(list[i]);
? - Haglandvar
的内容,但是代码还是不起作用:https://imgur.com/a/wuXHw - Haglandjobj["someprop"]
能否在JSON中找到值。JSON是一种分层表示法,不知道整个结构就无法给出正确的答案...如果您不想发布真实的JSON,则可以打开文档并阅读它们。这是我们根据您提供的信息所能提供的全部内容。 - Eser