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

时间:2018-11-14 15:39:48

标签: c# json deserialization

您好,我一直想将这个json响应反现实化,然后遍历对象,以便我可以将它们保存在数据库中,但始终会收到此错误。 这是我的课程

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; }
}

//这是Json响应

[
    {
        "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"
    }]

这是我尝试反序列化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);
}

我如何取消对json响应的匹配以匹配我的班级

2 个答案:

答案 0 :(得分:2)

您有两个问题。

首先是您的类不能准确表示您的Json。

这两个问题属性在您的RootObject类中:

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

您的json将这些显示为单个对象,而不是对象数组。您的代码应改为:

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

第二个问题是您的反序列化代码。您的json是对象的列表,而不是单个对象。因此您的代码应为:

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

工作小提琴here

答案 1 :(得分:0)

如@maccettura所建议,您可能必须执行以下操作:

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

这是因为您要反序列化一系列根对象而不是单个对象。