c# - 无法反序列化当前的 JSON 数组(例如 [1,2,3])

标签 c# arrays json serialization json.net

我正在尝试读取我的 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);

    }

但是我遇到了这个错误的问题:

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(内容);

有什么办法可以解决这个错误吗?

最佳答案

我认为您遇到的问题是您的 JSON 是一个对象列表,它与您的根类没有直接关系。

var content 看起来像这样(我假设):

[
  {
    "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。

因此,如果您尝试以下操作,它应该会起作用:

  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 列表,则需要进行迭代。


我继续在控制台应用程序中对此进行了测试,运行良好。

enter image description here

关于c# - 无法反序列化当前的 JSON 数组(例如 [1,2,3]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21885243/

相关文章:

c# - .NET Core HttpRequest header 优先级是什么?

c# - 在 LINQ 格式之间转换

c# - DTO 不应该知道业务对象中的枚举

python - 如何在 python 中创建列表数组?

Javascript:如何从数组中清除未定义的值

php - 数据不在 php json web api 中获取

c# - Entity Framework 中的并发异常

c++ - 在不创建任何类型对象的情况下声明 vector 和指针

java - 如何在Java中获取深度嵌套的JSON对象

c# - 如何使用 JSON 嵌套对象