【问题标题】:Current JsonReader item is not an object当前 JsonReader 项目不是对象
【发布时间】: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


【解决方案1】:

我遇到了一个非常相似的问题。我的 JObject.Parse(json) 对我不起作用的原因是因为我的 Json 有一个开头“[”和一个结尾“]”。为了使它工作,我不得不删除这两个字符。我会检查您的 Json 并确保它以 { 开头并以 } 结尾。

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

jsonResult = jsonResult.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' });

【讨论】:

  • 这解决了我的问题。 API 返回包含在[] 中的 JSON。非常感谢!
  • 嵌套数组的情况呢?解析器是否太弱以至于我必须检测每个子项并确保它们是对象而不是数组?
  • 这个有一个名字.. 这两个神奇的字符 [ 和 ] 意味着您正在查看一个 json-array... 而不是一个由 { 和 }
【解决方案2】:

我有类似的问题。
返回的 JSON 是 Array/List 但不是 Object。
相反,我使用 JArray.Parse 并且它可以工作。

jArray = JArray.Parse(content);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多