当前位置:  开发笔记 > 编程语言 > 正文

使用ID作为名称从json对象创建强类型c#对象

如何解决《使用ID作为名称从json对象创建强类型c#对象》经验,为你挑选了1个好方法。

我正在尝试将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 Stats { get; set; }
}

我很确定WebinarPerformanceStats是问题,但我不知道从哪里开始.我需要改变什么才能获得

NewtonSoft.Json.JsonConvert.DeserializeObject(theJsonResponse)

上班?



1> dbc..:

使您的根对象成为字典:

var dictionary = JsonConvert.DeserializeObject>(theJsonResponse);

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

var dictionary = JsonConvert.DeserializeObject>(theJsonResponse);

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

推荐阅读
我我檬檬我我186
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有