当前JsonReader项不是对象

yer*_*rpy 7 c# parsing json unit-testing json.net

首先,我创建了一个应用程序,然后开始对其进行测试(知道这不是一种好方法),在进行解析等工作时一切正常,但是经过几次测试后却出现了错误:

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;
}
Run Code Online (Sandbox Code Playgroud)

任何想法有什么问题吗?顺便说一句。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":[]}]
Run Code Online (Sandbox Code Playgroud)

小智 10

我有类似的问题。
返回的JSON是数组/列表,但不是对象。
相反,我使用了JArray.Parse,它可以工作。

jArray = JArray.Parse(content);
Run Code Online (Sandbox Code Playgroud)


小智 3

我遇到了一个非常相似的问题。我的 JObject.Parse(json) 对我不起作用的原因是因为我的 Json 有一个开头“[”和一个结尾“]”。为了让它发挥作用,我必须删除这两个角色。我会检查你的 Json 并确保它以 { 开头并以 } 结尾。

对我来说,我删除了第一个和最后一个字符。

jsonResult = jsonResult.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' });
Run Code Online (Sandbox Code Playgroud)

  • 这个有一个名字.. 这两个神奇的字符 [ 和 ] 意味着你正在查看一个 json 数组...而不是一个由 { 和 } 括起来的 json 对象 (4认同)