Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果找到数组而不是对象USIC,如何为json字符串提供异常处理#_C#_Json_C# 3.0_Choetl - Fatal编程技术网

C# 如果找到数组而不是对象USIC,如何为json字符串提供异常处理#

C# 如果找到数组而不是对象USIC,如何为json字符串提供异常处理#,c#,json,c#-3.0,choetl,C#,Json,C# 3.0,Choetl,我有一个json文本文件,如下所示: [ { "productId":"17213812", "returnPolicies": { "user":"Regular Guest" } }, { "productId":"17813832", "returnPolicies":[] } ] 我得到了returnPolicies的错误,因为它有不同的属性。但是

我有一个json文本文件,如下所示:

[
    { 
        "productId":"17213812",
        "returnPolicies": {
            "user":"Regular Guest"
        }
    },
    {
        "productId":"17813832",
        "returnPolicies":[]
    }
] 
我得到了
returnPolicies
的错误,因为它有不同的属性。但是我只想要
returnpoliciesuser
,所以如果我使用
e.returnpolicies.user
它将显示第二个数组的错误。。
任何人都知道如何跳过
“ReturnPolicys”:[]
,只搜索
e.ReturnPolicys.user(“ReturnPolicys”:{“user”:“常规来宾”})

这就是你可以做到的

using (var jr = new ChoJSONReader("sample6.json")
    .WithField("ProductId", jsonPath: "$.productId")
    .WithField("User", jsonPath: "$.returnPolicies.user")
    )
{
    foreach (var item in jr)
        Console.WriteLine(item.ProductId + " " + item.User);
}

这就是你能做到的

using (var jr = new ChoJSONReader("sample6.json")
    .WithField("ProductId", jsonPath: "$.productId")
    .WithField("User", jsonPath: "$.returnPolicies.user")
    )
{
    foreach (var item in jr)
        Console.WriteLine(item.ProductId + " " + item.User);
}

问题不是它们有不同的属性,而是一个是对象,另一个是数组。请添加读取JSON的代码。您使用的是
ChoJSONReader
?那么如何解决它呢?任何建议Yup im使用ChoJSONReader我不知道
ChoJSONReader
,但是对于其他库,您需要更改json,使其既使用列表也使用对象。问题不是它们具有不同的属性,而是一个是对象,另一个是数组。请添加读取json的代码。您使用的是
ChoJSONReader
?那么如何解决它呢?任何建议Yup im使用ChoJSONReader我不知道
ChoJSONReader
,但是对于其他库,您需要更改json,既可以使用列表,也可以使用对象。