0

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
                }
            }
        }
}
2
  • 2
    The error is exactly what it says it is. In your JSON you have a property result that contains an object, with a property activities with a bunch more properties. That is not an array, so how is it supposed to deserialize it to one? You can either deserialize activities to a dictionary, or you can write your own converter to handle that property. Commented Sep 9, 2016 at 13:47
  • You don't have collections in JSON message but do have in DT object. Commented Sep 9, 2016 at 13:50

2 Answers 2

3

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; }
    }    
}
Sign up to request clarification or add additional context in comments.

6 Comments

Schedule is a collection too
@Plutonix: Thanx - rather it is a Dictionary as well. I will update my answer. I am just checking if DateTime for Start is the proper type.
Just think you need to put a [JsonProperty("activity_id_name")] on ActivityIdName property and [JsonProperty("activity_id")] on ActivityId property, or use something like this : migara.li/2016/01/09/json-net-easy-serialization
Thanks a bunch @rboe !! this did the trick, DateTime appears to be working so it should be the right type.
And your dictionaries could be of type <int, ...> (instead of string)
|
-1

Have you tried JSON.NET ? it's MIT licenced BTW.

1 Comment

This answer fails to adress the discrepancy between the code and the recieved JSON message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.