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

      <tfoot id='ysn5i'></tfoot>

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

          <bdo id='ysn5i'></bdo><ul id='ysn5i'></ul>
      2. 从 json 对象创建一个以 ID 为名称的强类型 c# 对象

        时间:2023-08-25

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

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

                    <tbody id='zDyIz'></tbody>
                2. <legend id='zDyIz'><style id='zDyIz'><dir id='zDyIz'><q id='zDyIz'></q></dir></style></legend>
                  本文介绍了从 json 对象创建一个以 ID 为名称的强类型 c# 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试将 API 用于知名的在线会议提供商.他们的其中一个 API 调用返回一个如下所示的对象.

                  I am trying to make use of the API for a well known online meeting provider. One of their API calls returns an object that looks like this.

                  {
                      "5234592":{
                      "pollsAndSurveys":{
                          "questionsAsked":1,
                          "surveyCount":0,
                          "percentageSurveysCompleted":0,
                          "percentagePollsCompleted":100,
                          "pollCount":2},
                      "attendance":{
                          "averageAttendanceTimeSeconds":253,
                          "averageInterestRating":0,
                          "averageAttentiveness":0,
                          "registrantCount":1,
                          "percentageAttendance":100}
                      },
                      "5235291":{
                      "pollsAndSurveys":{
                          "questionsAsked":2,
                          "surveyCount":0,
                          "percentageSurveysCompleted":0,
                          "percentagePollsCompleted":0,
                          "pollCount":0},
                      "attendance":{
                          "averageAttendanceTimeSeconds":83,
                          "averageInterestRating":0,
                          "averageAttentiveness":0,
                          "registrantCount":1,
                          "percentageAttendance":100}
                      }
                  }
                  

                  我正在尝试在 C# 中创建一个强类型对象,以便处理这些数据.我可以为 pollsAndSurveys 位和出勤位创建对象,但我不知道如何处理 id 号,在本例中为 5234592 &5235291,即会话的标识符.

                  I am trying to make a strongly typed object in C# so I can deal with this data. I can create objects for the pollsAndSurveys bit and the attendance bit but I don't know how to deal with the id number, in this case 5234592 & 5235291, that is the identifier for the session.

                  public class AttendanceStatistics
                  {
                      [JsonProperty(PropertyName = "registrantCount")]
                      public int RegistrantCount { get; set; }
                  
                      [JsonProperty(PropertyName = "percentageAttendance")]
                      public float PercentageAttendance{ get; set; }
                  
                      [JsonProperty(PropertyName = "averageInterestRating")]
                      public float AverageInterestRating { get; set; }
                  
                      [JsonProperty(PropertyName = "averageAttentiveness")]
                      public float AverageAttentiveness { get; set; }
                  
                      [JsonProperty(PropertyName = "averageAttendanceTimeSeconds")]
                      public float AverageAttendanceTimeSeconds { get; set; }
                  }
                  
                  public class PollsAndSurveysStatistics
                  {
                      [JsonProperty(PropertyName = "pollCount")]
                      public int PollCount { get; set; }
                  
                      [JsonProperty(PropertyName = "surveyCount")]
                      public float SurveyCount { get; set; }
                  
                      [JsonProperty(PropertyName = "questionsAsked")]
                      public int QuestionsAsked { get; set; }
                  
                      [JsonProperty(PropertyName = "percentagePollsCompleted")]
                      public float PercentagePollsCompleted { get; set; }
                  
                      [JsonProperty(PropertyName = "percentageSurveysCompleted")]
                      public float PercentageSurveysCompleted { get; set; }
                  }
                  
                  public class SessionPerformanceStats
                  {
                      [JsonProperty(PropertyName = "attendance")]
                      public AttendanceStatistics Attendance { get; set; }
                  
                      [JsonProperty(PropertyName = "pollsAndSurveys")]
                      public PollsAndSurveysStatistics PollsAndSurveys { get; set; }
                  }
                  
                  public class WebinarPerformanceStats
                  {
                      public List<SessionPerformanceStats> Stats { get; set; }
                  }
                  

                  我很确定 WebinarPerformanceStats 是问题所在,但我不知道从哪里开始.我必须改变什么才能得到

                  I am pretty sure that the WebinarPerformanceStats is the issue but I don't know where to go from here. What would I have to change to get

                  NewtonSoft.Json.JsonConvert.DeserializeObject<WebinarPerformanceStats>(theJsonResponse)

                  上班?

                  推荐答案

                  让你的根对象成为字典:

                  Make your root object be a dictionary:

                  var dictionary = JsonConvert.DeserializeObject<Dictionary<string, SessionPerformanceStats>>(theJsonResponse);
                  

                  Json.NET 将字典从 JSON 对象序列化到 JSON 对象,并将键转换为属性名称.在您的情况下,ID 号将被反序列化为字典键.如果您确定它们将始终是数字,则可以这样声明它们:

                  Json.NET serializes a dictionary from and to a JSON object, with the keys being converted to the property names. In your case, the ID numbers will be deserialized as the dictionary keys. If you are sure they will always be numbers, you could declare them as such:

                  var dictionary = JsonConvert.DeserializeObject<Dictionary<long, SessionPerformanceStats>>(theJsonResponse);
                  

                  参见序列化字典和反序列化字典

                  这篇关于从 json 对象创建一个以 ID 为名称的强类型 c# 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Newtonsoft Json 中的 TypeNameHandling 谨慎 下一篇:JSON.NET 错误检测到类型的自引用循环

                  相关文章

                  最新文章

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

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