C#解析Json格式数据小结

最近,遇到了一些不同的Json格式的数据,需要做不同处理才能转化为想要得到的结果,这里总结一下。

第一种形式:status中是{}形式,对象

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

针对以上格式的数据,创建如下的两个结构体或类

 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中是[]形式,数组

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

针对以上格式的数据,创建如下的两个结构体或类

 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": "部门经理",
        "image": "/2015/1/13/d2e2e3f2c2f8_2e4f5b.jpg",
        "id": 143375468
        }
     }

创建类:

 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数据:

 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         .......
原文地址:https://www.cnblogs.com/cdz-sky/p/4528556.html