我试图读取我的JSON结果。
这是我的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"}]
这是我的代码来读取结果
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);
}
但我遇到了这个错误:
无法将当前的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);
有什么方法可以帮助修复这个错误吗?
DeserializeObject
之前,var content
的内容是什么? - Dayan