C#字符串怎么转成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\\jsonmy1.txt");//Jsonstr函数读取json数据的文本txt
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
ToJsonMy list = js.Deserialize<ToJsonMy>(json); //将json数据转化为对象类型并赋值给list
string result = list.result;
string res_info = list.res_info;
string queryorder_info = list.res_info;
}
json包含对象数组 json数据:jsonmy4.txt
{"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","PayAmt":"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"}]}}