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':'哈哈'}}";

针对以上格式的数据,创建如下的两个结构体或类网址:yii666.com

     public struct Status
{
public string created_at { get; set; }
public string text { get; set; }
} public struct JsonData
{
public string name { get; set; }
public string province { get; set; }
public string city { get; set; }
public string location { get; set; }
public Status status;
}

输出结果:文章来源地址:https://www.yii666.com/article/764369.html

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中是[]形式,数组文章地址https://www.yii666.com/article/764369.html

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

针对以上格式的数据,创建如下的两个结构体或类文章来源地址https://www.yii666.com/article/764369.html网址:yii666.com<

   public struct Status
{
public string created_at { get; set; }
public string text { get; set; }
}
public struct JsonData2
{
public string name { get; set; }
public string province { get; set; }
public string city { get; set; }
public string location { get; set; }
public List<Status> status;
}

输出结果

      JavaScriptSerializer jsSerializer=new JavaScriptSerializer();
JsonData2 jd = jsSerializer.Deserialize<JsonData2>(json);
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));

项目应用:

json字符串:

    {
"depart_id": ,
"depart_name": "人事部",
"depart_source": "[{\"text\": \"\", \"type\": \"text\"},{\"text\": \"\", \"type\": \"image\"},{\"text\": \"\", \"type\": \"audio\"}]",
"staff": {
"name": "谭琳",
"title": "部门经理",
"image": "/2015/1/13/d2e2e3f2c2f8_2e4f5b.jpg",
"id":
}
}

创建类:

 public class DepatData
{
public int depart_id = ;
public string depart_name = "";
public string depart_source = "";
public StaffData staff =new StaffData(); public class StaffData
{
public string name = "";
public string title = "";
public string image = "";
public string id = "";
}
}

解析Json数据:

         DepatData d = JsonConvert.DeserializeObject<DepatData>(strJson);
List<Dictionary<string, string>> depart_source =
JsonConvert.DeserializeObject < List<Dictionary<string, string>>>(d.depart_source); //获取值
int depart_id = d.depart_id;
.......
string text = depart_source[]["text"];
string type = depart_source[]["type"];
.......

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png