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
}
}
}
}
resultthat contains an object, with a propertyactivitieswith a bunch more properties. That is not an array, so how is it supposed to deserialize it to one? You can either deserializeactivitiesto a dictionary, or you can write your own converter to handle that property.