无法反序列化当前的JSON数组(例如[1,2,3]) [英] Cannot deserialize the current JSON array (e.g. [1,2,3])
问题描述
我想读我的JSON结果。
I am trying to read my JSON result.
这是我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; }
}
这里是我的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"}]
下面是我的code读取结果
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 :
无法反序列化当前的JSON数组(例如[1,2,3])入式'avaris_cash_salepoint.BL.UniqueItemDataRootObject'因为类型
需要一个JSON对象(例如{名:值})进行反序列化
正确。要解决这个错误要么改变的JSON到一个JSON对象
(例如{名:值})或改变反序列化类型的数组或
实现一个集合接口的类型(例如ICollection的,
IList的),如列表,可以从JSON数组反序列化。
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.
在这行:
返回JsonConvert.DeserializeObject(内容);
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.
VAR含量
会是这个样子(我假设):
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])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!