Niels Peeren Niels Peeren - 2 months ago 17
C# Question

create c# object from json array

I am receiving an error when trying to convert my json array into an object.

I have a Json array with multiple layers like this :

{
"error":"0",
"result":{
"activities":{
"1":{
"activity_id":"15803",
"activity_id_name":"Ashtanga vinyasa",
"schedule":{
"1":{
"available":"30",
"start":"09:00:00"
}
}
},
"2":{
"activity_id":"15804",
"activity_id_name":"Yin/Yang",
"schedule":{
"1":{
"available":"30",
"start":"10:30:00"
}
}
},
"3":{
"activity_id":"15805",
"activity_id_name":"Stress relief",
"schedule":{
"1":{
"available":"30",
"start":"20:00:00"
}
}
}
},
"json_code":"2"
},
"id":null
}


From this i created an object that looks like this :

namespace LesRooster.Models
{
public class JsonGroup
{
public string Error { get; set; }
public List<Results> Result { get; set; }
public string Id { get; set; }

}

public class Results
{
public Activities YogaActivities { get; set; }
public int JsonCode { get; set; }
}

public class Activities
{
public int ActivityId { get; set; }
public string ActivityIdName { get; set; }
public Schedule LesSchedule { get; set; }
}

public class Schedule
{
public int Available { get; set; }
public DateTime Start { get; set; }
}

}


When I try to add the array to the object with the following line :

JsonGroup jgroup = JsonConvert.DeserializeObject<JsonGroup>(JsonArrayCode);


I am receiving the following error :


Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type
'System.Collections.Generic.List`1[LesRooster.Models.Results]' because
the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly.


Is there any way to resolve this? I would like to keep it as an object since it is easy to pass on in the view in my mvc5 application.

I have tried googling this but all solutions I have found are for single dimension json arrays.

The documentation of the API I request the json array from looks as follows :

json_code integer
Constant value: 2
activities hash
{
index : {
activity_id: integer
activity_id_name: string
schedule: hash
{
index : {
available: integer
start: time
}
}
}
}

Answer

The structure of your classes does not match the structure of the JSON - this results in an exception. I have made some changes to your classes to reflect the JSON structure.

The main difference is that Activities is not an Array but a Dictionary.

namespace LesRooster.Models
{
    public class JsonGroup
    {
        public string Error { get; set; }
        public Result Result { get; set; }
        public string Id { get; set; }

    }

    public class Result {
        public IDictionary<string, Activities> Activities {get; set;}
        public int JsonCode {get; set;}
    }        

    public class Activities
    {
        [JsonProperty("activity_id")]
        public int ActivityId { get; set; }
        [JsonProperty("activity_id_name")]
        public string ActivityIdName { get; set; }
        public IDictionary<string, Schedule> Schedule { get; set; }
    }

    public class Schedule
    {
        public int Available { get; set; }
        public DateTime Start { get; set; }
    }    
}