c# - 当前 JsonReader 项不是对象

标签 c# json unit-testing parsing json.net

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

Newtonsoft.Json.JsonReaderException : Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 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 是 Array/List 而不是 Object。
相反,我使用 JArray.Parse 并且它有效。

jArray = JArray.Parse(content);

关于c# - 当前 JsonReader 项不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40790217/

相关文章:

Python,从循环中删除具有特定键的JSON元素

java - 如何模拟和验证在子类中调用的 ScheduledExecutorService 方法

java - 在 ANT 构建工具中设置系统变量并使用 Syste.getEnv ("ABC_HOME"在 Java 中读取它)

c# - 代码契约与不使用它的框架一起

c# - 当前请求是否通过 Azure 部署通过 SSL 发出

c# - 如何在C#中使用NEST客户端的ElasticClient类中的Serialize方法?

PHP/JSON 解析

java - 如何告诉 Mockito Mock 注释返回一个不为 null 的模拟对象?

c# - 在 Docker 上运行 .NET framework console-app 服务

c# - 使用 mvc3 c# 将图表创建到 Excel 中