无法反序列化当前的 JSON 数组(例如 [1,2,3]) [英] Cannot deserialize the current JSON array (e.g. [1,2,3])
问题描述
我正在尝试读取我的 JSON 结果.
这是我的 RootObject
公共类RootObject{公共 int id { 获取;放;}public bool is_default { 获取;放;}公共字符串名称 { 获取;放;}公共整数数量{得到;放;}公共字符串股票{得到;放;}公共字符串 unit_cost { 获取;放;}}
这是我的 JSON 结果
[{"id": 3636, "is_default": true, "name": "Unit", "quantity": 1, "stock": "100000.00", "unit_cost": "0"}, {"id": 4592, "is_default": false, "name": "Bundle", "quantity": 5, "stock": "100000.00", "unit_cost": "0"}]
这是我读取结果的代码
public static RootObject GetItems(string user, string key, Int32 tid, Int32 pid){//根据地理位置参数自定义 URLvar url = string.Format(uniqueItemUrl, user, key, tid, pid);//同步消费var syncClient = new WebClient();var content = syncClient.DownloadString(url);return JsonConvert.DeserializeObject(content);}
但是我遇到了这个错误的问题:
<块引用>无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'avaris_cash_salepoint.BL.UniqueItemDataRootObject',因为类型需要一个 JSON 对象(例如 {"name":"value"})来反序列化正确.要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList) 像可以从 JSON 数组反序列化的 List.也可以将 JsonArrayAttribute 添加到类型中以强制它从 JSON 数组反序列化.路径 '',第 1 行,位置 1.
在这一行:return JsonConvert.DeserializeObject(content);
是否有帮助修复此错误?
我认为您遇到的问题是您的 JSON 是一个对象列表,它与您的根类没有直接关系.
>
var content
看起来像这样(我假设):
注意:使用 http://jsonviewer.stack.hu/ 来格式化您的 JSON.
因此,如果您尝试以下操作,它应该会起作用:
public static ListGetItems(string user, string key, Int32 tid, Int32 pid){//根据地理位置参数自定义 URLvar url = string.Format(uniqueItemUrl, user, key, tid, pid);//同步消费var syncClient = new WebClient();var content = syncClient.DownloadString(url);return JsonConvert.DeserializeObject>(content);}
如果您不想返回 RootObject
的列表,则需要进行迭代.
我继续在控制台应用中对此进行了测试,效果很好.
I am trying to read my JSON result.
Here is my RootObject
public class RootObject
{
public int id { get; set; }
public bool is_default { get; set; }
public string name { get; set; }
public int quantity { get; set; }
public string stock { get; set; }
public string unit_cost { get; set; }
}
Here is my JSON result
[{"id": 3636, "is_default": true, "name": "Unit", "quantity": 1, "stock": "100000.00", "unit_cost": "0"}, {"id": 4592, "is_default": false, "name": "Bundle", "quantity": 5, "stock": "100000.00", "unit_cost": "0"}]
Here is my code to read the result
public static RootObject GetItems(string user, string key, Int32 tid, Int32 pid)
{
// Customize URL according to geo location parameters
var url = string.Format(uniqueItemUrl, user, key, tid, pid);
// Syncronious Consumption
var syncClient = new WebClient();
var content = syncClient.DownloadString(url);
return JsonConvert.DeserializeObject<RootObject>(content);
}
But I am having a problem with this error :
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'avaris_cash_salepoint.BL.UniqueItemDataRootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.
at this line: return JsonConvert.DeserializeObject(content);
Any help to fix this error ?
I think the problem you're having is that your JSON is a list of objects when it comes in and it doesnt directly relate to your root class.
var content
would look something like this (i assume):
[
{
"id": 3636,
"is_default": true,
"name": "Unit",
"quantity": 1,
"stock": "100000.00",
"unit_cost": "0"
},
{
"id": 4592,
"is_default": false,
"name": "Bundle",
"quantity": 5,
"stock": "100000.00",
"unit_cost": "0"
}
]
Note: make use of http://jsonviewer.stack.hu/ to format your JSON.
So if you try the following it should work:
public static List<RootObject> GetItems(string user, string key, Int32 tid, Int32 pid)
{
// Customize URL according to geo location parameters
var url = string.Format(uniqueItemUrl, user, key, tid, pid);
// Syncronious Consumption
var syncClient = new WebClient();
var content = syncClient.DownloadString(url);
return JsonConvert.DeserializeObject<List<RootObject>>(content);
}
You will need to then iterate if you don't wish to return a list of RootObject
.
I went ahead and tested this in a Console app, worked fine.
这篇关于无法反序列化当前的 JSON 数组(例如 [1,2,3])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!