C# 如果找到数组而不是对象USIC,如何为json字符串提供异常处理#
我有一个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的错误,因为它有不同的属性。但是
[
{
"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,既可以使用列表,也可以使用对象。