c#.net4複雜json讀取
阿新 • • 發佈:2019-02-20
在寫軟體的時候,遇到負責的json字串,需要反序列化,方便讀取,我的想法是能反序列成jsonview一樣的效果。
下面一步一步來操作
一:我的json資料字串如下:
{"auction_config":{"act":[{"id":"1","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"2","tejiaIcon":"","tejiaMark":"","act_jj":"該商品為今日特價賣家特別推薦,請放心選購!","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"12","tejiaIcon":"","tejiaMark":"【新人專享免費領】","act_jj":"本商品為特惠拍拍新使用者免費領用。(信用為0的使用者)","qianggou_img1":"http:\/\/static.paipaiimg.com\/auction\/btn_draw2.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/auction\/btn_draw1.png","qianggou_des1":"此商品將於_開搶時間_開始領取,請點選按鈕先確認收貨地址。","qianggou_des2":"此商品已經開始領取,點選按鈕立即領取。","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"13","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"14","tejiaIcon":"","tejiaMark":"【週四搶購】","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"15","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"16","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"17","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"18","tejiaIcon":"http:\/\/static.paipaiimg.com\/assets\/auction\/v3\/icon_tezi.png","tejiaMark":"手機拍拍專搶","act_jj":"本商品為今日特價手機Q購專搶,假一賠三、限量特價!","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"11","tejiaIcon":"","tejiaMark":"","act_jj":"該商品為今日特價賣家特別推薦,請放心選購!","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_freeze.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"4","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"3","tejiaIcon":"","tejiaMark":"【試吃試用活動】","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/auction\/btn_draw2.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/auction\/btn_draw1.png","qianggou_des1":"此商品將於_開搶時間_開始領取,請點選按鈕先確認收貨地址。","qianggou_des2":"此商品已經開始領取,點選按鈕立即領取。","detail_iframe":"http:\/\/www.paipai.com\/buyervip\/page\/try\/try_intro.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"19","tejiaIcon":"http:\/\/static.paipaiimg.com\/assets\/auction\/v3\/icon_tezi.png","tejiaMark":"今日特價","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址。","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_tejia.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"64","tejiaIcon":"","tejiaMark":"測試","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang1.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/assets\/auction\/btn_qiang2.png","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址。","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始搶購","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_tejia.shtml","qg_address":"http:\/\/ext.paipai.com\/crazybuy\/address"},{"id":"6","tejiaIcon":"","tejiaMark":"【彩鑽新品發售】","act_jj":"","qianggou_img1":"http:\/\/pics2.paipaiimg.com\/update\/20110217\/index_154512599.jpg","qianggou_img2":"http:\/\/pics0.paipaiimg.com\/update\/20110217\/index_154518777.jpg","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即購買","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"7","tejiaIcon":"","tejiaMark":"【試用達人】","act_jj":"","qianggou_img1":"http:\/\/static.paipaiimg.com\/auction\/btn_draw2.png","qianggou_img2":"http:\/\/static.paipaiimg.com\/auction\/btn_draw1.png","qianggou_des1":"此商品將於_開搶時間_開始領取,請點選按鈕先確認收貨地址。","qianggou_des2":"此商品已經開始領取,點選按鈕立即領取。","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"},{"id":"8","tejiaIcon":"","tejiaMark":"","act_jj":"","qianggou_img1":"http:\/\/pics2.paipaiimg.com\/update\/20110217\/index_154512599.jpg","qianggou_img2":"http:\/\/pics0.paipaiimg.com\/update\/20110217\/index_154518777.jpg","qianggou_des1":"此商品將於_開搶時間_開始銷售,請點選按鈕先確認收貨地址","qianggou_des2":"此商品已經開始銷售,點選按鈕立即開始購買","detail_iframe":"http:\/\/www.paipai.com\/sale\/auction2\/oneaday_test.shtml","qg_address":"http:\/\/ext.paipai.com\/challenge\/address"}],"sub":[{"type":"0","link":"http:\/\/www.paipai.com\/sale\/sub.shtml","desc":"訂閱活動提醒資訊或郵件,掌握活動第一手資訊!","btn":"去訂閱"},{"type":"","link":"http:\/\/www.paipai.com\/sale\/sub.shtml","desc":"訂閱活動提醒資訊或郵件,掌握活動第一手資訊!","btn":"去訂閱"}],"descl": [{"retCode":"1010000","desc":"不給力哦,您可以休息一會,再來試試。"},{"retCode":"1010100","desc":"該商品不是活動商品"},{"retCode":"1010101","desc":"不能購買自己的商品"},{"retCode":"1010200","desc":"您操作的速度太快了,請您稍後再試。"},{"retCode":"1010300","desc":"本商品為拍拍新手專搶,您是拍拍網老使用者,可返回店鋪檢視更多優惠!"},{"retCode":"1010301","desc":"對不起!您的彩鑽當前為熄滅狀態,不能享有購買該商品的權利。可能原因是:<br\/>您是拍拍新人,未曾在拍拍上購物,彩鑽還不能點亮。<a href='http:\/\/www.paipai.com\/buyervip\/fresher\/?PTAG=30972.15.7\/'>現在就去購物>><\/a><br>您已經超過50天未在拍拍上購物,彩鑽狀態已熄滅,通過再次購物即可點亮彩鑽。<a href='http:\/\/www.paipai.com'>現在就去購物>><\/a>"},{"retCode":"http:\/\/auction2.paipai.com\/EBECDA4F00000000006B3A0406BA3F25?PTAG=40012.5.2&LOGINTAG=1&CacheTime=1283307415","desc":"三星原廠出品,外觀時尚,輕薄美智,是移動網際網路時代的必備數碼潮品"},{"retCode":"http:\/\/auction2.paipai.com\/EBECDA4F00000000006B3A0406BA3F25?PTAG=40012.5.2&LOGINTAG=1&CacheTime=1283307415","desc":"三星原廠出品,外觀時尚,輕薄美智,是移動網際網路時代的必備數碼潮品"},{"retCode":"http:\/\/auction2.paipai.com\/EBECDA4F00000000006B3A0406BA3F25?PTAG=40012.5.2&LOGINTAG=1&CacheTime=1283307415","desc":"三星原廠出品,外觀時尚,輕薄美智,是移動網際網路時代的必備數碼潮品"},{"retCode":"1010302","desc":"對不起,您當前的彩鑽等級不夠,暫時不能購買本商品。 請升級後再來購買! <a href='http:\/\/www.paipai.com\/buyervip\/privilege.shtml?PTAG=30972.2.1'>檢視我的彩鑽等級特權>><\/a>"},{"retCode":"1010303","desc":"您需要<a href='http:\/\/pay.qq.com\/music\/index.shtml?aid=pay.music&ADTAG=pay.service.music.self' target='_blank'>開通綠鑽<\/a>,才能購買該商品!<br\/>愛音樂,享受音樂,盡在綠鑽貴族!<a href='http:\/\/vip.music.qq.com\/v2\/privileges\/index.html' target='_blank'>檢視更多特權<\/a>"},{"retCode":"1010500","desc":"您在15天內有兩次搶下活動商品但沒有付款的記錄。<br\/>因此從 begin 到 end ,您被限制不能參加搶購活動。end 後,您可正常參加搶購。"},{"retCode":"1010501","desc":"您的搶購受限,請檢查您是否被處罰。"},{"retCode":"1010502","desc":"您的搶購受限,請檢查您是否被處罰。"},{"retCode":"1010600","desc":"您本次搶購未成功哦,失敗乃成功之母,再試試吧。"},{"retCode":"1010700","desc":"您本次搶購未成功哦,失敗乃成功之母,再試試吧。"},{"retCode":"1010800","desc":"不給力哦,您可以休息一會,再來試試。"},{"retCode":"1010801","desc":"此商品為限量特價商品,已經搶購成功的使用者,請搶購其他商品"},{"retCode":"1010802","desc":"此商品為限量特價商品,已經搶購成功的使用者,請搶購其他商品"},{"retCode":"1010900","desc":"您好,您想購買的商品剩餘數量不足,無法滿足您的購買需求,如果您要繼續購買該商品,請重新輸入商品數量。"},{"retCode":"1010901","desc":"不給力哦,您可以休息一會,再來試試。"},{"retCode":"1011000","desc":"系統發生未知錯誤。"},{"retCode":"1010304","desc":"QQ會員購物特權的專享商品,您需要先成為QQ會員才能享受。<br\/>做尊貴的QQ會員,盡享線上線下50多項特權。<a href='http:\/\/vip.qq.com\/freedom\/freedom.html' target='_blank'>檢視會員特權>><\/a><br\/><a href='http:\/\/pay.qq.com\/qqvip\/' target='_blank'>開通會員<\/a>"},{"retCode":"1010305","desc":"您需要開通紅鑽,才能購買該商品!<br\/><a href='http:\/\/pay.qq.com\/qqshow' target='_blank'>開通紅鑽<\/a>"},{"retCode":"1010306","desc":"你選購的商品是黃鑽專區特供,您需要開通黃鑽才可以購買哦<br\/>空間裝扮免費、應用大禮包、個性相簿、日誌信紙、黃鑽禮物等30餘項黃鑽精彩特權讓您玩轉空間,盡享尊貴!<a href='http:\/\/qzs.qq.com\/qzone\/mall\/v5\/web\/vip\/index_tell.html' target='_blank'>檢視黃鑽特權詳情>><\/a><br\/><a href='http:\/\/pay.qq.com\/qzone' target='_blank'>開通黃鑽<\/a>"},{"retCode":"1010399","desc":"對不起,您無許可權購買該商品,具體請檢視商品描述。"},{"retCode":"1010401","desc":"對不起,您無許可權購買該商品,具體請檢視商品描述。"},{"retCode":"1010102","desc":"此商品為手機Q購專搶商品。"},{"retCode":"1011100","desc":"不給力哦,您可以休息一會,再來試試。"},{"retCode":"1011101","desc":"對不起,您沒有報名,不能參加本次搶購!<br><br> <a href='http:\/\/ext.paipai.com\/bm\/first' target='_blank'>趕快報名吧<\/a>"},{"retCode":"1011200","desc":"對不起,您不是QQ飛車的玩家,不能參加本次搶購!"},{"retCode":"1011201","desc":"抱歉,此商品無法搶購,請您選購其它商品。"},{"retCode":"1010307","desc":"非常抱歉,只有藍鑽使用者才能購買此商品。<br\/>立即<a href='http:\/\/pay.qq.com\/qqgame\/index.shtml?aid=LZ.ACT.PAIPAI_LINK2&ADTAG=PAY.LZ.ACT.PAIPAI_LINK2' target='_blank'>開通藍鑽<\/a>,尊享購物特權!"}],"descl_act":[{"act":"10","retCode":"1010100","desc":"很抱歉!您購買的商品不在本次活動範圍內,請從店鋪進入購買!"},{"act":"10","retCode":"1010101","desc":"很抱歉!您是該商品賣家,不允許購買該商品!<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010200","desc":"您操作的速度太快了,請您稍後再試。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010600","desc":"您本次購買未成功哦,失敗乃成功之母,再試試吧。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010800","desc":"不給力哦,您可以休息一會,再來試試。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010700","desc":"您本次購買未成功哦,失敗乃成功之母,再試試吧。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010801","desc":"此商品為限量購買商品,已經搶購成功的使用者,請購買其他商品。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010900","desc":"您好,您想購買的商品剩餘數量不足,無法滿足您的購買需要,如果您要繼續購買該商品,請重新輸入商品數量。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"10","retCode":"1010901","desc":"不給力哦,您可以休息一會,再來試試。<br><br>進入<a href='http:\/\/shop.qq.com\/act\/sale\/happygo\/'>QQ商城歡樂購<\/a>或<a href='http:\/\/shop.qq.com'>QQ商城首頁<\/a>購買更多品牌正品!"},{"act":"64","retCode":"1011101","desc":"對不起,你尚未報名一元搶購,不能參加本次搶購。"}]}}}
我想將他反序列成這樣:
二:準備工作,首先c#裡操作json字串必須嚴格按規範 "名稱":"值" 這樣的形式,即名稱和值必須雙引號。
如果不是,檢查並格式成這樣:
/// <summary> /// 規範的json建對是這樣:"名稱":"值",本方法將json字串中的名稱新增雙引號 /// </summary> /// <param name="jsonString">json字串</param> /// <returns></returns> public static string JsonFormat(string jsonString) { //(?<=:)[\w]+(?=,) 以:開頭,以,結尾中間的字元 return Regex.Replace(jsonString, @"[\w]+(?=:)", "\"$0\"");//連續字母或數字以:結尾且不包含: }
這裡給出的json字串不需要進行這一步。呵呵。
三:c#是強型別,要想順利將json字串反序列成樹,必須定義對應的實體類:
可以自己定義,也可以自動生成
///http://jsonclassgenerator.codeplex.com/ ----開源的winform工具。
///http://json2csharp.com/---一款線上轉化工具,大家看名字就知道了。
public class Act { public string id { get; set; } public string tejiaIcon { get; set; } public string tejiaMark { get; set; } public string act_jj { get; set; } public string qianggou_img1 { get; set; } public string qianggou_img2 { get; set; } public string qianggou_des1 { get; set; } public string qianggou_des2 { get; set; } public string detail_iframe { get; set; } public string qg_address { get; set; } } public class Sub { public string type { get; set; } public string link { get; set; } public string desc { get; set; } public string btn { get; set; } } public class Descl { public string retCode { get; set; } public string desc { get; set; } } public class DesclAct { public string act { get; set; } public string retCode { get; set; } public string desc { get; set; } } public class AuctionConfig { public List<Act> act { get; set; } public List<Sub> sub { get; set; } public List<Descl> descl { get; set; } public List<DesclAct> descl_act { get; set; } } public class Price2mailfee { public string act { get; set; } } public class RootObject { public AuctionConfig auction_config { get; set; } public Price2mailfee price2mailfee { get; set; } }
四:vs2010反序列化方法:
/// <summary>
/// 將json字串轉化為方法實體類
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T JsonToObject<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
五:開始反序列化,
首先定義實體類
string a = json字串
RootObject ac = new RootObject();
ac = JsonToObject<RootObject>(a);
這樣就反序列成了這樣
使用起來非常方便