Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a strongly typed c# object from json object with ID as the name

Tags:

json

c#

json.net

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}
    }
}

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; }
}

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)

to work?

like image 942
kaelle Avatar asked Dec 10 '15 23:12

kaelle


People also ask

How is C strongly typed?

Is C a strongly or weakly typed programming language? C is strongly typed in that the variable type must be specified when declaring the variable. However, C can also be regarded as weakly typed because users can convert data types through a cast and without compiler errors.

Why is C not a strongly typed language?

Is there a more definitive answer? To a C programmer strong typing means pressing the keys harder. C is on the weak side in the typing continuum.

Is C strongly type?

A programming language that requires a variable to be defined, and the variable it is. For example, C is a strongly typed language. When declaring the variable, you must also specify the variable type.


1 Answers

Make your root object be a dictionary:

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

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);

See Serialize a Dictionary and Deserialize a Dictionary

like image 80
dbc Avatar answered Sep 24 '22 23:09

dbc