国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C#解析Json格式數(shù)據(jù)小結

2019-11-17 02:07:18
字體:
來源:轉載
供稿:網(wǎng)友

C#解析Json格式數(shù)據(jù)小結

最近,遇到了一些不同的Json格式的數(shù)據(jù),需要做不同處理才能轉化為想要得到的結果,這里總結一下。

第一種形式:status中是{}形式,對象

string json =                 @"{'name':'Tom','

針對以上格式的數(shù)據(jù),創(chuàng)建如下的兩個結構體或類

 1     public struct Status 2     { 3         public string created_at { get; set; } 4         public string text { get; set; } 5     } 6  7     public struct JsonData 8     { 9         public string name { get; set; }10         public string province { get; set; }11         public string city { get; set; }12         public string location { get; set; }13         public Status status;14     }

輸出結果:

1   javaScriptSerializer jsSerializer=new JavascriptSerializer();2    JsonData jd = jsSerializer.Deserialize<JsonData>(json);3    Response.Write(string.Format("name={0};province={1};city={2};location={3};status={4};",jd.name,jd.province,jd.city,jd.location,jd.status.created_at+jd.status.text));

第二種形式:status中是[]形式,數(shù)組

string json =                @"{'name':'Tom','province':'32','city':'1','location':'江蘇 南京','status':[{'created_at':'Thu Feb 26 21:04:34 +0800 2015','text':'哈哈'}]}";

針對以上格式的數(shù)據(jù),創(chuàng)建如下的兩個結構體或類

 1   public struct Status 2     { 3         public string created_at { get; set; } 4         public string text { get; set; } 5     } 6     public struct JsonData2 7     { 8         public string name { get; set; } 9         public string province { get; set; }10         public string city { get; set; }11         public string location { get; set; }12         public List<Status> status;13     }

輸出結果

1      JavaScriptSerializer jsSerializer=new JavaScriptSerializer();2      JsonData2 jd = jsSerializer.Deserialize<JsonData2>(json);3      Response.Write(string.Format("name={0};province={1};city={2};location={3};status={4};",jd.name,jd.province,jd.city,jd.location,jd.status[0].created_at+jd.status[0].text));

項目應用:

json字符串:

    {    "depart_id": 5,    "depart_name": "人事部",    "depart_source": "[{/"text/": /"/", /"type/": /"text/"},{/"text/": /"/", /"type/": /"image/"},{/"text/": /"/", /"type/": /"audio/"}]",    "staff": {        "name": "譚琳",        "title": "部門經(jīng)理",        "image": "/2015/1/13/d2e2e3f2c2f8_2e4f5b.jpg",        "id": 143375468        }     }

創(chuàng)建類:

 1 public class DepatData 2 { 3     public int depart_id = 0; 4     public string depart_name = ""; 5     public string depart_source = ""; 6     public StaffData staff =new StaffData(); 7  8     public class StaffData 9     {10         public string name = "";11         public string title = "";12         public string image = "";13         public string id = "";14     }15 }

解析Json數(shù)據(jù):

 1         DepatData d = JsonConvert.DeserializeObject<DepatData>(strJson); 2         List<Dictionary<string, string>> depart_source = 3             JsonConvert.DeserializeObject < List<Dictionary<string, string>>>(d.depart_source); 4          5         //獲取值 6         int depart_id = d.depart_id; 7         ....... 8         string text = depart_source[0]["text"]; 9         string type = depart_source[0]["type"];10         .......


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 调兵山市| 同仁县| 分宜县| 德江县| 乡宁县| 海丰县| 胶州市| 盐亭县| 桃江县| 杭州市| 辽阳市| 康乐县| 阜阳市| 汨罗市| 城固县| 永州市| 资阳市| 广昌县| 宜丰县| 永昌县| 克什克腾旗| 汕尾市| 尚志市| 洛南县| 广东省| 漾濞| 鄢陵县| 彰化县| 双城市| 灵川县| 德保县| 嵊州市| 宁武县| 通州市| 杭锦旗| 龙山县| 伊宁市| 汝城县| 奉节县| 饶阳县| 沙雅县|