简体   繁体   English

无法将当前json数组反序列化为类型

[英]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 这是我的样本Json,我发布到web api控制器

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

Here is my c# class 这是我的c#课程

    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...." 当我这样做时,我得到错误“无法将当前的json数组(例如1 2 3)反序列化为类型....”

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 我在堆栈和其他论坛上搜索了大部分建议是反序列化json数组。所以这就是我在我的c#代码中所做的

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

Note : value is the param where i get this json. 注意:value是我得到这个json的参数。

I tried some changes in my class files like doing idictionary <string,object> but that gives me the same error. 我在我的类文件中尝试了一些更改,比如执行idictionary <string,object>但这给了我同样的错误。

Since i am working with angularjs i tried the following 由于我正在使用angularjs,我尝试了以下内容

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 在此先感谢您,祝大家新年快乐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. mods的注意事项:我知道这是一个重复的问题,但大多数问题都没有在json中处理数组。

The JSON that you show in your question, in not a valid JSON: 您在问题中显示的JSON,而不是有效的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. manageaptarray是一个无效的数组语法,也是JSON语法,Doc1和Doc2不是有效值,可能它们是一个字符串,所以它们应该在双引号之间。

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. 您必须首先修复JSON,然后您可以查看我的答案将JSON反序列化为Object C# ,它显示如何使用Visual Studio 2013/15在C#类中转换有效的JSON。

A valid JSON should be like that: 有效的JSON应该是这样的:

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

And the C# class that match this, is: 与此匹配的C#类是:

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: 然后,您可以使用以下命令将此JSON反序列化为类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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