• <bdo id='TjPYd'></bdo><ul id='TjPYd'></ul>
  1. <i id='TjPYd'><tr id='TjPYd'><dt id='TjPYd'><q id='TjPYd'><span id='TjPYd'><b id='TjPYd'><form id='TjPYd'><ins id='TjPYd'></ins><ul id='TjPYd'></ul><sub id='TjPYd'></sub></form><legend id='TjPYd'></legend><bdo id='TjPYd'><pre id='TjPYd'><center id='TjPYd'></center></pre></bdo></b><th id='TjPYd'></th></span></q></dt></tr></i><div id='TjPYd'><tfoot id='TjPYd'></tfoot><dl id='TjPYd'><fieldset id='TjPYd'></fieldset></dl></div>

    <small id='TjPYd'></small><noframes id='TjPYd'>

    1. <legend id='TjPYd'><style id='TjPYd'><dir id='TjPYd'><q id='TjPYd'></q></dir></style></legend><tfoot id='TjPYd'></tfoot>

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

      时间:2023-08-24

      1. <legend id='8k6vP'><style id='8k6vP'><dir id='8k6vP'><q id='8k6vP'></q></dir></style></legend>
          <bdo id='8k6vP'></bdo><ul id='8k6vP'></ul>

              <tbody id='8k6vP'></tbody>

          1. <tfoot id='8k6vP'></tfoot>

            <i id='8k6vP'><tr id='8k6vP'><dt id='8k6vP'><q id='8k6vP'><span id='8k6vP'><b id='8k6vP'><form id='8k6vP'><ins id='8k6vP'></ins><ul id='8k6vP'></ul><sub id='8k6vP'></sub></form><legend id='8k6vP'></legend><bdo id='8k6vP'><pre id='8k6vP'><center id='8k6vP'></center></pre></bdo></b><th id='8k6vP'></th></span></q></dt></tr></i><div id='8k6vP'><tfoot id='8k6vP'></tfoot><dl id='8k6vP'><fieldset id='8k6vP'></fieldset></dl></div>
              • <small id='8k6vP'></small><noframes id='8k6vP'>

                本文介绍了无法反序列化当前 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])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Json.NET 用最少的小数位序列化浮点/双精度,即没 下一篇:Newtonsoft Json.Net 序列化 JObject 不会忽略空值,即使

                相关文章

                最新文章

                <legend id='7Q2G4'><style id='7Q2G4'><dir id='7Q2G4'><q id='7Q2G4'></q></dir></style></legend>
              • <tfoot id='7Q2G4'></tfoot>

                    • <bdo id='7Q2G4'></bdo><ul id='7Q2G4'></ul>

                    <small id='7Q2G4'></small><noframes id='7Q2G4'>

                    <i id='7Q2G4'><tr id='7Q2G4'><dt id='7Q2G4'><q id='7Q2G4'><span id='7Q2G4'><b id='7Q2G4'><form id='7Q2G4'><ins id='7Q2G4'></ins><ul id='7Q2G4'></ul><sub id='7Q2G4'></sub></form><legend id='7Q2G4'></legend><bdo id='7Q2G4'><pre id='7Q2G4'><center id='7Q2G4'></center></pre></bdo></b><th id='7Q2G4'></th></span></q></dt></tr></i><div id='7Q2G4'><tfoot id='7Q2G4'></tfoot><dl id='7Q2G4'><fieldset id='7Q2G4'></fieldset></dl></div>