从ID为名称的json对象创build一个强types的c#对象

我正在尝试使用一个知名的在线会议提供商的API。 他们的一个API调用返回一个看起来像这样的对象。

{ "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#中使用强types对象,所以我可以处理这些数据。 我可以为pollsAndSurveys位和出席位创build对象,但我不知道如何处理id号,在这种情况下是5234592&5235291,这是会话的标识符。

 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是问题,但我不知道从哪里去。 我需要改变什么才能得到

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

上class?

让你的根对象成为一个字典:

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

Json.NET将字典从一个JSON对象序列化,并将其转换为属性名称。 在你的情况下,ID号将被反序列化为字典键。 如果你确定他们将永远是数字,你可以声明他们:

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

请参阅序列化字典和反序列化字典