c#json数据解析——将字符串json格式数据转换成对象
⽹络中数据传输经常是xml或者json,现在做的⼀个项⽬之前调其他系统接⼝都是返回的xml格式,刚刚遇到⼀个返回json格式数据的接⼝,通过例⼦由易到难总结⼀下处理过程,希望能帮到和我⼀样开始不会的朋友。
1.json简单对象
json数据:
{"result":"0","res_info":"ok","queryorder_info":"info"}
我这⾥是以⽂件的形式保存在d盘json⽂件夹下。
构造对象:
public struct ToJsonMy
{
public string result { get; set; } //属性的名字,必须与json格式字符串中的"key"值⼀样。
public string res_info { get; set; }
public string queryorder_info { get; set; } }
转换过程:
public static void JsonMy()
{
string json = Jsonstr("D:\\json\\");//Jsonstr函数读取json数据的⽂本txt
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化⼀个能够序列化数据的类
ToJsonMy list = js.Deserialize<ToJsonMy>(json); //将json数据转化为对象类型并赋值给listjson值的类型有哪些
string result = sult;
string res_info = s_info;
string queryorder_info = s_info;
}
2.json还包含对象
json数据:
{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}
构造对象:
public struct ToJsonMy2
{
public string result { get; set; }
public string res_info { get; set; }
public queryorder_info queryorder_info;
}
public struct queryorder_info
{
public string order_num { get; set; }
public string orderdetail { get; set; }
};
转换过程:
public static void JsonMy2()
{
string json = Jsonstr("D:\\json\\");
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化⼀个能够序列化数据的类
ToJsonMy2 list = js.Deserialize<ToJsonMy2>(json); //将json数据转化为对象类型并赋值给list
string result = sult;
string res_info = s_info;
string order_num = list.der_num;
string orderdetail = list.derdetail;
}
3.json包含对象数组 json数据:jsonmy
<
{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":[{"CFTUin":"769839263","CancelDeadline":"2013-09-12
23:00:00","CheckInDate":"2013-09-12 00:00:00","CheckOutDate":"2013-09-13
00:00:00","CityID":"0101","CurrencyCode":"RMB","HotelID":"00301105","HotelName":"乐家连锁(北京天坛南门店)(原速8酒店(北京天坛南门店)","ListID":"1000000005201308280002999652","Pay
Amt":"228","PayType":"0","RommsCnt":"1","SPTransID":"65202157","State":"4"}, {"CFTUin":"248486133","CancelDeadline":"2013-10-13 23:00:00","CheckInDate":"2013-10-13 00:00:00","CheckOutDate":"2013-10-18
00:00:00","CityID":"0201","CurrencyCode":"RMB","HotelID":"10201314","HotelName":"上海凯顿酒店","ListID":"1000000005201308280002999413","PayAmt":"1140","PayType":"0","RommsCnt":"1","SPTransID":"65197226","State":"4"}]}}
构造对象:
public struct ToJsonMy3
{
public string result { get; set; }
public string res_info { get; set; }
public queryorder_info queryorder_info;
}
public struct queryorder_info
{
public string order_num { get; set; }
public List<orderdetail> orderdetail;//数组处理
};
public struct orderdetail
{
public string CFTUin { get; set; }
public string CancelDeadline { get; set; }
public string CheckInDate { get; set; }
public string CheckOutDate { get; set; }
public string CityID { get; set; }
public string CurrencyCode { get; set; }
public string HotelID { get; set; }
public string HotelName { get; set; }
public string ListID { get; set; }
public string PayAmt { get; set; }
public string PayType { get; set; }
public string RommsCnt { get; set; }
public string SPTransID { get; set; }
public string State { get; set; }
};
转换过程:
public static void JsonMy4()
{
string json = Jsonstr("D:\\json\\");
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化⼀个能够序列化数据的类
ToJsonMy3 list = js.Deserialize<ToJsonMy3>(json); //将json数据转化为对象类型并赋值给list string result = sult;
string res_info = s_info;
string order_num = list.der_num;
List<orderdetail> orderdetail = list.derdetail;
string CFTUin = orderdetail[0].CFTUin;
string HotelName = orderdetail[0].HotelName;
string ListID = orderdetail[1].ListID;
string State = orderdetail[2].State;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论