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

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

  • <legend id='GI7v7'><style id='GI7v7'><dir id='GI7v7'><q id='GI7v7'></q></dir></style></legend>

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

    1. <tfoot id='GI7v7'></tfoot>

        使用未知对象名称反序列化 JSON

        时间:2023-08-23

            <tbody id='x8mOJ'></tbody>

          <tfoot id='x8mOJ'></tfoot>
          1. <small id='x8mOJ'></small><noframes id='x8mOJ'>

                <legend id='x8mOJ'><style id='x8mOJ'><dir id='x8mOJ'><q id='x8mOJ'></q></dir></style></legend>
                  <bdo id='x8mOJ'></bdo><ul id='x8mOJ'></ul>

                  <i id='x8mOJ'><tr id='x8mOJ'><dt id='x8mOJ'><q id='x8mOJ'><span id='x8mOJ'><b id='x8mOJ'><form id='x8mOJ'><ins id='x8mOJ'></ins><ul id='x8mOJ'></ul><sub id='x8mOJ'></sub></form><legend id='x8mOJ'></legend><bdo id='x8mOJ'><pre id='x8mOJ'><center id='x8mOJ'></center></pre></bdo></b><th id='x8mOJ'></th></span></q></dt></tr></i><div id='x8mOJ'><tfoot id='x8mOJ'></tfoot><dl id='x8mOJ'><fieldset id='x8mOJ'></fieldset></dl></div>
                  本文介绍了使用未知对象名称反序列化 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试反序列化来自 API 的 JSON 响应.JSON 如下所示(MAC 地址和位置已更改):

                  I'm trying to deserialize a JSON response from an API. The JSON looks like this (MAC addresses and location are altered):

                  {
                  "body" : [{
                          "_id" : "da:87:54:26:53:97",
                          "place" : {
                              "location" : [-23.334961, 47.398349],
                              "altitude" : 30,
                              "timezone" : "Europe/London"
                          },
                          "mark" : 3,
                          "measures" : {
                              "f2:bf:a7:6f:e7:e8" : {
                                  "res" : {
                                      "1469997248" : [20.4, 66]
                                  },
                                  "type" : ["temperature", "humidity"]
                              },
                              "42:b7:48:59:7c:4b" : {
                                  "res" : {
                                      "1469997263" : [1016.7]
                                  },
                                  "type" : ["pressure"]
                              }
                          },
                          "modules" : ["f2:bf:a7:6f:e7:e8"]
                      }
                  ],
                  "status" : "ok",
                  "time_exec" : 0.034152030944824,
                  "time_server" : 1469997417
                  }
                  

                  问题在于措施块.由于对象的名称正在更改,我不知道如何将其正确反序列化为 C# 对象.我在这里发现了一个与使用字典的解决方案类似的问题,但是如果我这样尝试,我只会得到 null目录.

                  The problem is the measures block. Since the name of the object is changing, I don't know how to deserialize it properly into a C# object. I found a similiar problem on here with the solution to use a dictonary, however if I try it this way I just get null directory.

                  这是我的反序列化方法:

                  This is my deserialize method:

                  APIResponse apiResponse = JsonConvert.DeserializeObject<APIResponse>(await content.ReadAsStringAsync());
                  

                  这是 APIResponse 类:

                  And this is the APIResponse class:

                  public class APIResponse
                  {
                      public Body[] body { get; set; }
                      public string status { get; set; }
                      public float time_exec { get; set; }
                      public int time_server { get; set; }
                  }
                  
                  public class Body
                  {
                      public string _id { get; set; }
                      public Place place { get; set; }
                      public int mark { get; set; }
                      public Measures measures { get; set; }
                      public string[] modules { get; set; }
                  }
                  
                  public class Place
                  {
                      public float[] location { get; set; }
                      public float altitude { get; set; }
                      public string timezone { get; set; }
                  }
                  
                  public class Measures
                  {
                     public Dictionary<string, SingleModule> singlemodules{ get; set; }
                  }
                  
                  public class SingleModule
                  {
                      public Res res { get; set; }
                      public string[] type { get; set; }
                  }
                  
                  public class Res
                  {
                      public MeasuredData measuredData { get; set; }
                  }
                  public class MeasuredData
                  {
                      public float[] values { get; set; }
                  }
                  

                  有什么方法可以轻松地正确反序列化这些措施?

                  Any way to properly derserialize the measures easily?

                  推荐答案

                  看起来你应该能够摆脱你的 Measures 类.相反,将字典直接放入您的 Body 类:

                  It looks like you should be able to just get rid of your Measures class. Instead, put the dictionary straight into your Body class:

                  public class Body
                  {
                      public string _id { get; set; }
                      public Place place { get; set; }
                      public int mark { get; set; }
                      public Dictionary<string, SingleModule> measures { get; set; }
                      public string[] modules { get; set; }
                  }
                  

                  另外,我强烈建议您的属性遵循 .NET 命名约定,并使用 [JsonProperty("measures")] 等向 Json.NET 指示如何翻译您的属性属性转换成 JSON.

                  As a separate matter, I'd strongly recommend following .NET naming conventions for your properties, and using [JsonProperty("measures")] etc to indicate to Json.NET how to translate your properties into JSON.

                  这篇关于使用未知对象名称反序列化 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何将类字段与 System.Text.Json.JsonSerializer 一起使 下一篇:使用 JSON.NET 的序列化字段的顺序

                  相关文章

                  最新文章

                    <bdo id='YouOF'></bdo><ul id='YouOF'></ul>
                  1. <small id='YouOF'></small><noframes id='YouOF'>

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

                    1. <tfoot id='YouOF'></tfoot>

                      <legend id='YouOF'><style id='YouOF'><dir id='YouOF'><q id='YouOF'></q></dir></style></legend>