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

    1. <small id='taLwF'></small><noframes id='taLwF'>

      <legend id='taLwF'><style id='taLwF'><dir id='taLwF'><q id='taLwF'></q></dir></style></legend><tfoot id='taLwF'></tfoot>

      1. 得到“因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化"反序列化 Json 对象时出

        时间:2023-05-22
        <i id='c4FDs'><tr id='c4FDs'><dt id='c4FDs'><q id='c4FDs'><span id='c4FDs'><b id='c4FDs'><form id='c4FDs'><ins id='c4FDs'></ins><ul id='c4FDs'></ul><sub id='c4FDs'></sub></form><legend id='c4FDs'></legend><bdo id='c4FDs'><pre id='c4FDs'><center id='c4FDs'></center></pre></bdo></b><th id='c4FDs'></th></span></q></dt></tr></i><div id='c4FDs'><tfoot id='c4FDs'></tfoot><dl id='c4FDs'><fieldset id='c4FDs'></fieldset></dl></div>
          <tfoot id='c4FDs'></tfoot><legend id='c4FDs'><style id='c4FDs'><dir id='c4FDs'><q id='c4FDs'></q></dir></style></legend>

            <tbody id='c4FDs'></tbody>

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

            • <bdo id='c4FDs'></bdo><ul id='c4FDs'></ul>
                • 本文介绍了得到“因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化"反序列化 Json 对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  请帮助我.我在哪里缺少信息?我需要反序列化以下 JSON 字符串.

                  Please help me. Where I am missing info? I need to deserialize the following JSON string.

                  {"results":[{"series":[{"name":"PWR_00000555","columns":["time","last"],"values":[["1970-01-01T00:00:00Z",72]]}]}]}

                  {"results":[{"series":[{"name":"PWR_00000555","columns":["time","last"],"values":[["1970-01-01T00:00:00Z",72]]}]}]}

                  为此,我定义了我的类:

                  For this, I have defined my class:

                  public class Serie
                  {
                      public Serie()
                      {
                          this.Points = new List<object[]>();
                      }
                  
                      [JsonProperty(PropertyName = "results")]
                      public string results { get; set; }
                  
                      [JsonProperty(PropertyName = "series")]
                      public string sries { get; set; }
                  
                  
                      [JsonProperty(PropertyName = "name")]
                      public string Name { get; set; }
                  
                      [JsonProperty(PropertyName = "columns")]
                      public string[] ColumnNames { get; set; }
                  
                      [JsonProperty(PropertyName = "values")]
                      public List<object[]> Points { get; set; }
                  

                  但是当我尝试使用反序列化器时,它给出了一个异常.

                  But when I try using the de-serializer, it gives an exception.

                  {"无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[InfluxDB.Serie]',因为该类型需要要正确反序列化的 JSON 数组(例如 [1,2,3]). 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或将反序列化类型更改为可以从 JSON 对象反序列化的普通 .NET 类型(例如,不是整数之类的原始类型,不是数组或 List 之类的集合类型).也可以将 JsonObjectAttribute 添加到该类型以强制其反序列化JSON 对象. 路径结果",第 2 行,位置 12."}

                  {"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[InfluxDB.Serie]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'results', line 2, position 12."}

                  推荐答案

                  您收到此错误是因为您的 JSON 是分层的,而您的类本质上是扁平的.如果您使用 JSONLint.com 来验证和重新格式化 JSON,您可以更好地看到结构:

                  You are getting this error because your JSON is hierarchical while your class is essentially flat. If you use JSONLint.com to validate and reformat the JSON, you can see the structure better:

                  {
                      "results": [
                          {
                              "series": [
                                  {
                                      "name": "PWR_00000555",
                                      "columns": [
                                          "time",
                                          "last"
                                      ],
                                      "values": [
                                          [
                                              "1970-01-01T00:00:00Z",
                                              72
                                          ]
                                      ]
                                  }
                              ]
                          }
                      ]
                  }
                  

                  这对应于以下类结构(我最初使用 json2csharp.com 生成,然后手动编辑添加[JsonProperty] 属性):

                  This corresponds to the following class structure (which I initially generated using json2csharp.com, then manually edited to add the [JsonProperty] attributes):

                  public class RootObject
                  {
                      [JsonProperty("results")]
                      public List<Result> Results { get; set; }
                  }
                  
                  public class Result
                  {
                      [JsonProperty("series")]
                      public List<Series> Series { get; set; }
                  }
                  
                  public class Series
                  {
                      [JsonProperty("name")]
                      public string Name { get; set; }
                      [JsonProperty("columns")]
                      public List<string> ColumnNames { get; set; }
                      [JsonProperty("values")]
                      public List<List<object>> Points { get; set; }
                  }
                  

                  您可以像这样将您的 JSON 反序列化为上述类结构:

                  You can deserialize your JSON into the above class structure like this:

                  var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
                  

                  小提琴:https://dotnetfiddle.net/50Z64s

                  这篇关于得到“因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化"反序列化 Json 对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:序列化无符号整数 (ulong) 数组时 Json.NET 崩溃 下一篇:找不到方法'无效 Newtonsoft.Json.Serialization.DefaultContractRe

                  相关文章

                    <bdo id='qo1Tq'></bdo><ul id='qo1Tq'></ul>

                • <small id='qo1Tq'></small><noframes id='qo1Tq'>

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