将复杂的 Json 转换为对象

我有以下 JSON 响应:


{

"data": [

{

  "ac_conditions": "[{\"ac_condition_group_id\":156570,\"ac_condition_group_name\":\"\u0413\u0440\u0443\u043f\u043f\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439  1\",\"id\":311790,\"ac_parameter\":\"utm_source\",\"ac_operator\":\"=\",\"value\":\"google_rem\",\"is_negative\":false},{\"ac_condition_group_id\":156570,\"ac_condition_group_name\":\"\u0413\u0440\u0443\u043f\u043f\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439  1\",\"id\":275094,\"ac_parameter\":\"utm_source\",\"ac_operator\":\"=\",\"value\":\"yandex_retargeting\",\"is_negative\":false}]",

  "is_dt_enabled": true,

  "ac_id": 162866,

  "site_blocks": "[{\"id\":324164,\"name\":\"\u041d\u043e\u043c\u0435\u0440 1 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435\",\"phone_type\":\"virtual\",\"numb\":\"74950238629\",\"forward_numb\":null,\"is_dt_enabled\":true,\"dt_number_pool_numbers\":[\"74951828912\",\"74950324045\",\"74950324046\",\"74950324043\",\"74950324037\",\"74951828907\",\"74950324048\",\"74950324049\",\"74951523589\",\"74953239984\"]}]"

}

],

"success": true

}

我创建了一个类来反现实化它


    public class Condition 

    {

        public bool success;

        public List<Data> data;

        public class Data 

        {

            public string ac_conditions;                

            public int ac_id; 

            public bool is_dt_enabled;

            public string site_blocks;


        };

    }

运行良好。我需要的是取消实现元素 ac_conditions 和 site_blocks。我创建了一个新类,但出现异常(System.String 无法转换为 List)

       

我在代码中使用这一行来反序列化 JSON 响应


JsonConvert.DeserializeObject<Models.Condition>(string_Condition);


DIEA
浏览 59回答 1
1回答

Smart猫小萌

您的 JSON 属性“site_blocks”是一个包含序列化 JSON 数据的字符串值。因此,您需要第二步来解包/反序列化数据。如果您可以更改生成响应的方式,您可以在那里修复它(在 site_blocks 中返回 JSON 并且不返回字符串)例如(使用 Json.net 和第二次解析运行的结果存储在 site_blocks_parsed 中)public class Condition&nbsp;{&nbsp; &nbsp; public bool success;&nbsp; &nbsp; public List<Data> data;&nbsp; &nbsp; public class Data&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string ac_conditions;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public int ac_id;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public bool is_dt_enabled;&nbsp; &nbsp; &nbsp; &nbsp; public string site_blocks;&nbsp; &nbsp; &nbsp; &nbsp; public List<SiteBlock> site_blocks_parsed;&nbsp; &nbsp; &nbsp; &nbsp; public class ConditionCamp&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int ac_condition_group_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string ac_condition_group_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string ac_parameter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string ac_operator;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool is_negative;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class SiteBlock&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string phone_type;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string numb;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string forward_numb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool is_dt_enabled;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string dt_number_pool_numbers;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };}...var condition = JsonConvert.DeserializeObject<Condition>(jsonString);foreach (var data in condition.data) {&nbsp; data.site_blocks_parsed = JsonConvert.DeserializeObject<List<SiteBlock>>(data.site_blocks);}
打开App,查看更多内容
随时随地看视频慕课网APP