【问题标题】:Create a strongly typed c# object from json object with ID as the name从 json 对象创建一个以 ID 为名称的强类型 c# 对象
【发布时间】:2015-12-10 23:19:59
【问题描述】:

我正在尝试将 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# 中创建一个强类型对象,以便处理这些数据。我可以为 pollsAndSurveys 位和出勤位创建对象,但我不知道如何处理 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 是问题所在,但我不知道从哪里开始。我必须改变什么才能得到 ​​p>

NewtonSoft.Json.JsonConvert.DeserializeObject&lt;WebinarPerformanceStats&gt;(theJsonResponse)

上班?

【问题讨论】:

    标签: c# json json.net


    【解决方案1】:

    让你的根对象成为字典:

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

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

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

    Serialize a DictionaryDeserialize a Dictionary

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 2023-01-20
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多