与ID作为名称,创建从JSON对象一个强类型的C#对象 [英] Create a strongly typed c# object from json object with ID as the name

查看:129
本文介绍了与ID作为名称,创建从JSON对象一个强类型的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对象,用钥匙被转换到属性名。在你的情况下,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);

请参阅的序列化字典反序列化词典

这篇关于与ID作为名称,创建从JSON对象一个强类型的C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆