无法将Json数组反序列化为c#中的模型对象

时间:2018-05-25 07:03:42

标签: c# json

我正在尝试通过url从常量联系web api获取信息,并将结果从json数组解析为模型对象。但模型对象始终为null。获取的信息成功存储在变量中,但永远不会转换为对象。以下是我的所作所为!

class.css

 public class CCModel
    {
        public string id { get; set; }
        public List<Address> addresses { get; set; }
        public List<List> lists { get; set; }
        public string cell_phone { get; set; }
        public string company_name { get; set; }
        public bool confirmed { get; set; }
        public List<EmailAddress> email_addresses { get; set; }
        public string fax { get; set; }
        public string first_name { get; set; }
        public string home_phone { get; set; }
        public string job_title { get; set; }
        public string last_name { get; set; }
        public string middle_name { get; set; }
        public string prefix_name { get; set; }
        public string work_phone { get; set; }
        public DateTime created_date { get; set; }
        public string source_details { get; set; }
        public string source { get; set; }
        public string status { get; set; }
        public List<CustomFields> custom_fields { get; set; }
        public string modified_date { get; set; }
        public List<Notes> notes { get; set; }


    }

转换

  var a = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
                                JObject jsonResponse = JObject.Parse(a.ToString());
                                lstModel = new CCModel();
                                lstModel = JsonConvert.DeserializeObject<CCModel>(response.Content.ReadAsStringAsync().Result); // here it is always null

并且var a保存此

"results": [
    {
      "id": "1154958965",
      "status": "ACTIVE",
      "fax": "",
      "addresses": [],
      "notes": [],
      "confirmed": false,
      "lists": [
        {
          "id": "1597641299",
          "status": "ACTIVE"
        }
      ],
      "source": "Site Owner",
      "email_addresses": [
        {
          "id": "55071710-5db2-11e8-bced-d4ae5275509e",
          "status": "ACTIVE",
          "confirm_status": "NO_CONFIRMATION_REQUIRED",
          "opt_in_source": "ACTION_BY_OWNER",
          "opt_in_date": "2018-05-22T11:21:54Z",
          "email_address": "XXX"
        }
      ],
      "prefix_name": "",
      "first_name": "XXX",
      "middle_name": "",
      "last_name": "XXX",
      "job_title": "Developer",
      "company_name": "Codeit",
      "home_phone": "",
      "work_phone": "",
      "cell_phone": "",
      "custom_fields": [],
      "created_date": "2018-05-22T11:21:54Z",
      "modified_date": "2018-05-23T11:02:09Z",
      "source_details": ""
    }
  ]

注意:请记住,只提取了一个结果,因此不将其解析为对象模型列表

1 个答案:

答案 0 :(得分:0)

var a = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
                                JObject jsonResponse = JObject.Parse(a.ToString());
                                lstModel = new CCModel();
                                lstModel = JsonConvert.DeserializeObject<CCModel>(a); <-------------

一旦你有了,那么为什么你需要再次获得相同的响应。试试这个。 &#34; var a&#34;已经在记忆中了。

相关问题