【C#】【JSON】JSON转C#的写法(含嵌套对象)

来源:互联网 发布:大数据对教育的影响 编辑:程序博客网 时间:2024/05/16 05:34

JSON字符串反序列化时,碰到JSON嵌套的问题,所以写这些当做个笔记。

#1 JSON字符串

 string voucherJson = "{\"voucherList\":   [  {  \"voucherName\":\"团购代金券测试testhahah1测试\",  \"voucherID\":\"9907\",  \"voucherMoney\":\"10000\",  \"voucherValue\":\"20\",  \"getDateBegin\":\"2015-09-10\",  \"getDateEnd\":\"2015-09-15\",  \"useDateBegin\":\"2015-09-11\",  \"useDateEnd\":\"2015-09-21\",  \"voucherDept\":\"财务部\",  \"applationBy\":\"Test\",  \"area\":\"[上海]\",  \"acitictyJson\":[  {  \"actName\":\"12月11日上团携手圣象地板首次专场团购会\",  \"actTime\":\"2014-06-17\",  \"area\":\"上海\",  \"actType\":\"专场\",  \"buiness\":\"建材\"  },  {  \"actName\":\"12月11日蒙娜丽莎瓷砖专场团购会\",  \"actTime\":\"2014-06-20\",  \"area\":\"上海\",  \"actType\":\"专场\",  \"buiness\":\"建材\"  },  {  \"actName\":\"书香门地地板专场\",  \"actTime\":\"2014-12-13\",  \"area\":\"上海\",  \"actType\":\"专场\",  \"buiness\":\"建材\"  }  ],  \"merchantJson\": [],  \"remark\":\"备注备注备注备注备注备注备注备注备注备注备注备注备注备注\",  \"applicationDept\":\"财务部\",  \"appByEmail\":\"test@test.com\",  \"unplanned\":\"false\",  \"workId\":\"\"  },  {  \"voucherName\":\"团购代金券测试testhahah2测试\",  \"voucherID\":\"9908\",  \"voucherMoney\":\"10000\",  \"voucherValue\":\"20\",  \"getDateBegin\":\"2015-09-10\",  \"getDateEnd\":\"2015-09-15\",  \"useDateBegin\":\"2015-09-11\",  \"useDateEnd\":\"2015-09-21\",  \"voucherDept\":\"财务部\",  \"applationBy\":\"Test\",  \"area\":\"[上海]\",  \"acitictyJson\":[  {  \"actName\":\"12月11日上团携手圣象地板首次专场团购会\",  \"actTime\":\"2014-06-17\",  \"area\":\"上海\",  \"actType\":\"专场\",  \"buiness\":\"建材\"  },  {  \"actName\":\"12月11日蒙娜丽莎瓷砖专场团购会\",  \"actTime\":\"2014-06-20\",  \"area\":\"上海\",  \"actType\":\"专场\",  \"buiness\":\"建材\"  },  {  \"actName\":\"书香门地地板专场\",  \"actTime\":\"2014-12-13\",  \"area\":\"上海\",  \"actType\":\"专场\",  \"buiness\":\"建材\"  }  ],  \"merchantJson\": [],  \"remark\":\"备注备注备注备注备注备注备注备注备注备注备注备注备注备注\",  \"applicationDept\":\"财务部\",  \"appByEmail\":\"test@test.com\",  \"unplanned\":\"false\",  \"workId\":\"\"  }   ]  }";

#2 JSON字符串转C#对象基本使用

基本使用,也就是不通过辅助实体类,直接通过JSON.NET去反序列化JSON字符串,而且序列化后的对象一般都是Dictionary,因为JSON本身就是Key-Value的格式。只是这样一来,就处理不了该JSON字符串中某一Value仍是一个JSON字符串,或者是还需要处理的字符串。不过程序员实现方法是万变的,所以这种方式,有时也有用处,比如我就直接存储这些嵌套的Value值到数据库,而不再次进行嵌套转换。
如下为实现:
string voucherJson = "{\"voucherList\": [{\"voucherName\":\"团购代金券测试testhahah1测试\",\"voucherID\":\"9907\",\"voucherMoney\":\"10000\",\"voucherValue\":\"20\",\"getDateBegin\":\"2015-09-10\",\"getDateEnd\":\"2015-09-15\",\"useDateBegin\":\"2015-09-11\",\"useDateEnd\":\"2015-09-21\",\"voucherDept\":\"财务部\",\"applationBy\":\"Test\",\"area\":\"[上海]\",\"acitictyJson\":[{\"actName\":\"12月11日上团携手圣象地板首次专场团购会\",\"actTime\":\"2014-06-17\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"12月11日蒙娜丽莎瓷砖专场团购会\",\"actTime\":\"2014-06-20\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"书香门地地板专场\",\"actTime\":\"2014-12-13\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"}],\"merchantJson\": [],\"remark\":\"备注备注备注备注备注备注备注备注备注备注备注备注备注备注\",\"applicationDept\":\"财务部\",\"appByEmail\":\"test@test.com\",\"unplanned\":\"false\",\"workId\":\"\"},{\"voucherName\":\"团购代金券测试testhahah2测试\",\"voucherID\":\"9908\",\"voucherMoney\":\"10000\",\"voucherValue\":\"20\",\"getDateBegin\":\"2015-09-10\",\"getDateEnd\":\"2015-09-15\",\"useDateBegin\":\"2015-09-11\",\"useDateEnd\":\"2015-09-21\",\"voucherDept\":\"财务部\",\"applationBy\":\"Test\",\"area\":\"[上海]\",\"acitictyJson\":[{\"actName\":\"12月11日上团携手圣象地板首次专场团购会\",\"actTime\":\"2014-06-17\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"12月11日蒙娜丽莎瓷砖专场团购会\",\"actTime\":\"2014-06-20\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"书香门地地板专场\",\"actTime\":\"2014-12-13\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"}],\"merchantJson\": [],\"remark\":\"备注备注备注备注备注备注备注备注备注备注备注备注备注备注\",\"applicationDept\":\"财务部\",\"appByEmail\":\"test@test.com\",\"unplanned\":\"false\",\"workId\":\"\"} ]}";
//参考voucherJson,可以看到最外层的Key -<span style="font-family: Arial, Helvetica, sans-serif;">voucherList</span>值是一个数组,因此我在这里转成了List<Object>,而且一般JSON转换不需要辅助实体类的时候,Dictionary中的Value值类型Object,一般都是因JSON字符串格式而变的Dictionary<string, List<object>> voucherDict = JsonConvert.DeserializeObject<Dictionary<string, List<object>>>(voucherJson);            foreach (var voucher in voucherDict["voucherList"])            {                Dictionary<string, object> voucherDetailDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(voucher.ToString());                //操作voucherDetailDict                var a = voucherDetailDict["acitictyJson"];                var b = voucherDetailDict["area"];            }

#3 JSON字符串转C#对象嵌套使用

嵌套使用,由于Value值中还有JSON字符串需要转换,而JSON转换时一般都是将之转成Object(string)类型,这时候一般不能再转,或者通过拼接Value组成新的JSON字符串进行转换(不建议),或者也可以通过Split字符串(不建议)。
这时候,一般都需要一个辅助实体类Voucher,其类结构需要根据JSON字符串具体分析,如下为我的辅助实体类Voucher结构,和JSON字符串中一致:

    /// <summary>    /// Assist Class for JSON Converter    /// </summary>    public class Voucher    {        public string voucherName;        public string voucherID;        public decimal voucherMoney;        public string voucherValue;        public DateTime getDateBegin;        public DateTime getDateEnd;        public DateTime useDateBegin;        public DateTime useDateEnd;        public string voucherDept;        public string applationBy;        public string area;        public List<object> acitictyJson;        public List<object> merchantJson;        public string remark;        public string applicationDept;    }


JSON字符串转换实现及使用方式如下:
string voucherJson = "{\"voucherList\": [{\"voucherName\":\"团购代金券测试testhahah1测试\",\"voucherID\":\"9907\",\"voucherMoney\":\"10000\",\"voucherValue\":\"20\",\"getDateBegin\":\"2015-09-10\",\"getDateEnd\":\"2015-09-15\",\"useDateBegin\":\"2015-09-11\",\"useDateEnd\":\"2015-09-21\",\"voucherDept\":\"财务部\",\"applationBy\":\"Test\",\"area\":\"[上海]\",\"acitictyJson\":[{\"actName\":\"12月11日上团携手圣象地板首次专场团购会\",\"actTime\":\"2014-06-17\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"12月11日蒙娜丽莎瓷砖专场团购会\",\"actTime\":\"2014-06-20\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"书香门地地板专场\",\"actTime\":\"2014-12-13\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"}],\"merchantJson\": [],\"remark\":\"备注备注备注备注备注备注备注备注备注备注备注备注备注备注\",\"applicationDept\":\"财务部\",\"appByEmail\":\"test@test.com\",\"unplanned\":\"false\",\"workId\":\"\"},{\"voucherName\":\"团购代金券测试testhahah2测试\",\"voucherID\":\"9908\",\"voucherMoney\":\"10000\",\"voucherValue\":\"20\",\"getDateBegin\":\"2015-09-10\",\"getDateEnd\":\"2015-09-15\",\"useDateBegin\":\"2015-09-11\",\"useDateEnd\":\"2015-09-21\",\"voucherDept\":\"财务部\",\"applationBy\":\"Test\",\"area\":\"[上海]\",\"acitictyJson\":[{\"actName\":\"12月11日上团携手圣象地板首次专场团购会\",\"actTime\":\"2014-06-17\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"12月11日蒙娜丽莎瓷砖专场团购会\",\"actTime\":\"2014-06-20\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"},{\"actName\":\"书香门地地板专场\",\"actTime\":\"2014-12-13\",\"area\":\"上海\",\"actType\":\"专场\",\"buiness\":\"建材\"}],\"merchantJson\": [],\"remark\":\"备注备注备注备注备注备注备注备注备注备注备注备注备注备注\",\"applicationDept\":\"财务部\",\"appByEmail\":\"test@test.com\",\"unplanned\":\"false\",\"workId\":\"\"} ]}"; Dictionary<string, List<Voucher>> voucherDict = JsonConvert.DeserializeObject<Dictionary<string, List<Voucher>>>(voucherJson);            foreach (var voucher in voucherDict["voucherList"])            {                //处理其他的属性                var a = voucher.area;//?a的结果是[上海],                var b = voucher.applationBy;                //处理嵌套acitictyJson                foreach (var aciticty in voucher.acitictyJson)                {                    Dictionary<string, object> acitictyDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(aciticty.ToString());                    //用法                    var a1 = acitictyDict["actName"];                    var a2 = acitictyDict["actTime"];                    var a3 = acitictyDict["area"];                    var a4 = acitictyDict["actType"];                    var a5 = acitictyDict["buiness"];                }                //处理嵌套的merchantJson                foreach (var merchant in voucher.merchantJson)                {                    Dictionary<string, object> merchantDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(merchant.ToString());                    //用法                    var a1 = merchantDict["可能有的Key"];                }            }



1 0
原创粉丝点击