【发布时间】:2017-04-08 23:35:15
【问题描述】:
首先我创建了一个应用程序,然后我开始对其进行测试(知道这不是好方法),解析等一切正常,但在我做了一些测试后出现错误:
Newtonsoft.Json.JsonReaderException:从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是对象:StartArray。路径'',第 1 行,位置 1。
jObject = JObject.Parse(content); 和 arrayList = JArray.Parse(content); 出现错误
internal JObject DoParse(string content)
{
JObject jObject = new JObject();
if (content != null)
{
if (content.Contains("Unable"))
{
MessageBox.Show("Not found.", "Error");
}
else
{
jObject = JObject.Parse(content);
}
}
return jObject;
}
internal JArray DoParseOnList(string content)
{
JArray arrayList = new JArray();
if (content != null)
{
if (content.Contains("Unable"))
{
MessageBox.Show("Not found.", "Error");
}
else
{
arrayList = JArray.Parse(content);
}
}
else { }
return arrayList;
}
任何想法有什么问题吗?
顺便提一句。 string content 是我从服务器获取的 json。
提前致谢!
JSON
Test Name: SetGroup
Test Outcome: Failed
Result Message: SetUp : Newtonsoft.Json.JsonReaderException : Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Result StandardOutput: [{"id":6208,"name":"test"},{"id":6315,"name":"jPOD v144 Testing"},{"id":6306,"name":"iButton Issue"},{"id":6424,"name":"Hybrid"}]
[{"id":6208,"name":"test"},{"id":6315,"name":"jPOD v144 Testing"},{"id":6306,"name":"iButton Issue"},{"id":6424,"name":"Hybrid"}]
[{"enabled":true,"scriptVersion":199,"configVersion":3,"name":"LMU3030 Hybrid Car Test based on 64.112 add ignition on-off"},{"enabled":true,"scriptVersion":199,"configVersion":2,"name":"LMU3030 Hybrid Car Test based on 50.106"},{"enabled":true,"scriptVersion":199,"configVersion":1,"name":"Hybrid car LMU 3030 Ignition test","description":""},{"enabled":true,"scriptVersion":64,"configVersion":113,"name":"based on 64.112 Engineering Build from calamp"},{"enabled":true,"scriptVersion":61,"configVersion":106},{"enabled":true,"scriptVersion":38,"configVersion":117},{"enabled":true,"scriptVersion":184,"configVersion":0},{"enabled":true,"scriptVersion":13,"configVersion":54},{"enabled":true,"scriptVersion":23,"configVersion":105,"name":"PULS Redirect to PROD","description":"Changes just Param 2320 to maint.vehicle-location.com"}]
[]
[{"message":"Not Implemented","vbusDeviceFiles":[],"vbusFileHistories":[]}]
【问题讨论】:
-
拥有您的 JSON 以及这似乎是导致问题的原因而不是 C# 代码会很有用。
-
你能发布你从服务器获得的json吗?根据错误,您提供给 JObject.Parse() 和 JArray.Parse() 的内容不合适,即在第一种情况下不是 JObject,在第二种情况下不是 Jarray。
-
在调试其他所有内容之前,使用jsonlint.com等服务验证您的JSON
-
我确实用我得到的 Json 进行了编辑
-
@DerpyUnKnown 不,这不是为了解决您的问题。它旨在确定您的问题出在哪里,即问题是您的 JSON 还是您的 C# 代码。您需要修复生成 JSON 服务器端的任何问题,我们无能为力。
标签: c# json unit-testing parsing json.net