反序列化HTTP响应-获取空值

时间:2018-09-18 14:43:38

标签: c# http

我想使用Newtonsoft JsonConvert反序列化下面对类的HTTP响应。但是,当我尝试反序列化时,我得到的是空值。我曾经使用json2csharp生成一个类,但是对于这个“ @ odata.etag”字段,它为我提供了一个名为public string __invalid_name__@odata.etag { get; set; }的字段,该字段可能不正确。

我可以从不需要的负载中删除该字段吗?另外,有人可以帮助我如何删除此字段。或者,我可以使用类中的正确字段反序列化现有有效负载吗?

 - Payload(response contains an array of 5000, below is a sample of one)

    {
            "@odata.etag": "W/\"201865260\"",
            "new_ispioneer": false,
            "new_emailconfirmed": true,
            "merged": false,
            "msdynhcp_unsubscribeurl": null,
            "emailaddress1": "xxx@xxx.net",
            "new_emailverificationlastsent": null,
            "new_alphaplayer": false,
            "isbackofficecustomer": false,
            "_owningbusinessunit_value": "d80d1db5-c580-e611-80e3-c4346bac697c",
            "_owninguser_value": "157924d5-f69c-e611-80e6-c4346bac6938",
            "lastname": "xxx@xxx.net",
            "donotpostalmail": false,
            "marketingonly": false,
            "donotphone": false,
            "preferredcontactmethodcode": 1
        }

下面是我的课程

public class contact
{
    public string __invalid_name__@odata.etag { get; set; }
    public bool new_ispioneer { get; set; }
    public bool new_emailconfirmed { get; set; }
    public bool merged { get; set; }
    public object msdynhcp_unsubscribeurl { get; set; }
    public string emailaddress1 { get; set; }
    public object new_emailverificationlastsent { get; set; }
    public bool new_alphaplayer { get; set; }
    public bool isbackofficecustomer { get; set; }
    public string _owningbusinessunit_value { get; set; }
    public string _owninguser_value { get; set; }
    public string lastname { get; set; }
    public bool donotpostalmail { get; set; }
    public bool marketingonly { get; set; }
    public bool donotphone { get; set; }
    public int preferredcontactmethodcode { get; set; }
}

public class ContactList
    {
        [JsonProperty("Contact")]

        public IList<Contact> Contact { get; set; }
    }

我正在使用代码尝试反序列化

if (createResponse.StatusCode == HttpStatusCode.OK) //200  
                {
                    var obj = JsonConvert.DeserializeObject<JObject>(createResponse.Content.ReadAsStringAsync().Result);
                    var Raw = obj.SelectToken("value");
                    var collection = JsonConvert.DeserializeObject<List<ContactList>>(Raw.ToString());
}

集合包含一个对象列表,每个对象都为空。

0 个答案:

没有答案