问题描述
我正在尝试读取我的 JSON 结果.
I am trying to read my JSON result.
这是我的根对象
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; }
}
这是我的 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)
{
// 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 :
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型avaris_cash_salepoint.BL.UniqueItemDataRootObject",因为该类型需要一个 JSON 对象(例如 {"name":"value"})来反序列化正确.要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"}) 或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection,IList),例如可以从 JSON 数组反序列化的 List.JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化.路径'',第 1 行,位置 1.
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.
在这一行:return JsonConvert.DeserializeObject(content);
at this line: return JsonConvert.DeserializeObject(content);
有什么帮助可以解决这个错误吗?
Any help to fix this error ?
推荐答案
我认为你遇到的问题是你的 JSON 是一个对象列表,它与你的根类没有直接关系.
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.
p>
var content
看起来像这样(我假设):
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"
}
]
注意:使用 http://jsonviewer.stack.hu/ 来格式化您的 JSON.
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);
}
如果您不希望返回 RootObject
列表,则需要进行迭代.
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])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!