简体   繁体   English

无法在C#中反序列化当前JSON对象

[英]Cannot deserialize the current JSON object in C#

Hello i have been tyring to deserealize this json response and then loop through the objects so i can save them in my database but keeps getting this error. 您好,我一直在尝试去散播此json响应,然后循环遍历这些对象,以便我可以将它们保存在我的数据库中,但始终收到此错误。 Here are my classes 这是我的课

public class Details
{
    public string ConID { get; set; }
    public string Name { get; set; }
    public string LA { get; set; }
    public string Sector { get; set; }
    public string Category { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    public object ContactPerson { get; set; }
    public object ContactPhone { get; set; }
    public object ContactEmail { get; set; }
    public object BcNumber { get; set; }
    public string CID { get; set; }
}

public class Notice
{
    public string NoticeID { get; set; }
}

public class RootObject
{
    public List<Details> details { get; set; }
    public List<Notice> Notice { get; set; }
    public string ResponseCode { get; set; }
    public string ResponseMessage { get; set; }
}

//Here is the Json Response //这是Json Response

[
    {
        "details": {
            "ConID": "1427093",
            "Name": "David Mark",
            "LA": "LAGOS",
            "Sector": "SERVICES",
            "Category": "LARGE",
            "Email": "",
            "Phone": "",
            "Address": "VI LAGOS",
            "ContactPerson": null,
            "ContactPhone": null,
            "ContactEmail": null,
            "BcNumber": null,
            "CID": "11111111111"
        },
        "Notice": {
            "NoticeID": "null"
        },
        "ResponseCode": "00",
        "ResponseMessage": "Successfull"
    },
 {
        "details": {
            "ConID": "1427093",
            "Name": "Samuel King",
            "LA": "NAIROBI",
            "Sector": "SERVICES",
            "Category": "SMALL",
            "Email": "",
            "Phone": "",
            "Address": "VI LAGOS",
            "ContactPerson": null,
            "ContactPhone": null,
            "ContactEmail": null,
            "BcNumber": null,
            "CID": "11112112121"
        },
        "Notice": {
            "NoticeID": "null"
        },
        "ResponseCode": "00",
        "ResponseMessage": "Successfull"
    }]

Here is the part i tried to deserialize the json 这是我试图反序列化json的部分

HttpClient webClient = new HttpClient();
Uri uri = new Uri("http://xxxxx");
HttpResponseMessage response = webClient.GetAsync(uri).Result;
if (response.IsSuccessStatusCode)
{
    var JSON = response.Content.ReadAsStringAsync().Result;
    var _Data = JsonConvert.DeserializeObject<RootObject>(JSON);
}

How do i desearlize that json response to match my class 我如何反序列化json响应以匹配我的班级

You have two issues. 你有两个问题。

The first is that your classes dont accurately represent your Json. 首先是您的班级不能准确代表您的Json。

The two problem properties are in your RootObject class: 这两个问题属性在您的RootObject类中:

public List<Details> details { get; set; }
public List<Notice> Notice { get; set; }

Your json shows these as a single object, not an array of objects. 您的json将这些显示为单个对象,而不是对象数组。 Your code should instead be: 您的代码应改为:

public Details details { get; set; }
public Notice Notice { get; set; }

The second problem is your deserializtion code. 第二个问题是您的反序列化代码。 Your json is a list of objects, not a single one. 您的json是对象列表 ,而不是单个对象。 So your code should be: 因此,您的代码应为:

List<RootObject> _Data = JsonConvert.DeserializeObject<List<RootObject>>(JSON); 

Working fiddle here 在这里工作

As @maccettura suggests, you might have to do something like this: 正如@maccettura所建议的,您可能必须执行以下操作:

var _Data = JsonConvert.DeserializeObject<List<RootObject>>(JSON);

This is because you want to deserialize a list of root objects, not a single object. 这是因为您要反序列化根对象列表,而不是单个对象。

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

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