简体   繁体   中英

Can't deserialize the current json array into type

I just can't find a proper solution for this problem though i came across the same topic which was asked before.

Here is my sample Json which i am posting to a web api controller

      {
      "AppointmentId ":2079,       
       "manageaptarray":[  
       "VanId":6,
       "Doctors":[
         {"Id":1,"Name":Doc1},
         {"Id":2,"Name":Doc2}
      ]          
      ]}

Here is my c# class

    public class ManageDoctorModel
{
    public int Id { get; set; }
    public int AppointmentId { get; set; }
    public DateTime? AppointmentAt { get; set; }
    public DateTime ScheduledAt { get; set; }
    public int? ModifiedBy { get; set; }
    public DateTime? ModifiedDateTime { get; set; }
    public Nullable<bool> rCreate { get; set; }
    public Nullable<bool> rUpdate { get; set; }
    public Nullable<bool> rDelete { get; set; }
    public Nullable<bool> rView { get; set; }
    public jsonarray[] manageaptarray { get; set; }
}



public class jsonarray
{
    public int VanId { get; set; }

    public string VanName { get; set; }

    public List<Doctorslist> Doctors { get; set; }
}
}

when i do so i get the error "cannot deserialize the current json array (eg 1 2 3 ) into type...."

I searched around stack & other forums most of the suggestion was to deserialize the json array.So here is what i did in my c# code

List<jsonarray> jsondata = JsonConvert.DeserializeObject<System.Collections.Generic.List<jsonarray>>(value.manageaptarray.ToString());

Note : value is the param where i get this json.

I tried some changes in my class files like doing idictionary <string,object> but that gives me the same error.

Since i am working with angularjs i tried the following

json.stringify(jsondata)
angular.tojson(jsondata)
json.parse(jsondata)

but no luck with that.Please help me out here.To what i have understood i think passing array within an array is the problem that is causing the trouble.So if anyone can help me how to figure this out it would be greatful so that i wont be wasting more time on this.

Thanks in advance and wish you all a very happy new year 2016 <3

Kind Note to mods : I know this is a repeative question but most of the questions aren't dealt with array of array in json.

The JSON that you show in your question, in not a valid JSON:

{
    "AppointmentId ":2079,       
    "manageaptarray": [  
        "VanId": 6,
        "Doctors": [
            {
                "Id":1,
                "Name": Doc1
            },
            {
                "Id":2,
                "Name":Doc2
            }
        ]          
    ]
}

The manageaptarray is a invalid array syntax, also in JSON syntax, Doc1 and Doc2 isn't a valid value, probably they are a string, so they should be between double quotes.

You must fix the JSON first, then you can look my answer Deserialize JSON into Object C# , that show how to convert a valid JSON in a C# class using Visual Studio 2013/15.

A valid JSON should be like that:

{
    "AppointmentId ":2079,       
    "manageaptarray": [  
        {
            "VanId": 6,
            "Doctors": [
                {
                    "Id":1,
                    "Name": "Doc1"
                },
                {
                    "Id":2,
                    "Name": "Doc2"
                }
            ]          
        }
    ]
}

And the C# class that match this, is:

public class Rootobject
{
    public int AppointmentId { get; set; }
    public Manageaptarray[] manageaptarray { get; set; }
}

public class Manageaptarray
{
    public int VanId { get; set; }
    public Doctor[] Doctors { get; set; }
}

public class Doctor
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Then you can Deserialize this JSON into a class now using:

Rootobject jsondata = JsonConvert.DeserializeObject<Rootobject>(jsonString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM