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

Cannot deserialize the current JSON array (e.g. [1,2,3])(无法反序列化当前 JSON 数组(例如 [1,2,3]))
本文介绍了无法反序列化当前 JSON 数组(例如 [1,2,3])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取我的 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])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)