如何将C#/.NET 将json字符串格式数据转换成对象

知道91 | 教程 | 2014-09-16 | 阅读:23240

网络中数据的传输方式通常是json格式,对于.NET程序json数据很不方便,因此我们需要将json格式数组转换成对象,有的是字符串属性简单对象,有的需要将json格式字符串转换为对象中还有对象的对象,有的却是要将json格式字符串转换为List对象。下面讲解对json数据转换成对象的操作。

json中包含的是只有字符串的简单对象

比如:

{"result":"0","res_info":"ok","queryorder_info":"info"}

首先构造对象,如下面的代码

public struct JsonClass
{
	public string result { get; set; }  //属性的名字,必须与json格式字符串中的"key"值一样。
	public string res_info { get; set; }
	public string queryorder_info { get; set; }   
}

将json字符串转换为JsonClass对象,将json字符放在json1.txt文本中

public static void JsonMy()
{
	string json = Jsonstr("D:\\temp\\json1.txt");//Jsonstr函数读取json数据的文本txt                             
	JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
	JsonClass list = js.Deserialize(json);    //将json数据转化为对象类型并赋值给list
	string result = list.result;                         
	string res_info = list.res_info;
	string queryorder_info = list.res_info;
}

对于注意JavaScriptSerializer对象要引用System.Web.Extensions

json字符串中的对象还包含对象

json格式字符串示例如下:

{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}

构造对象

public struct JsonClass2
{
	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; }
};

转换对象,将json字符放在json2.txt文本中

public static void JsonMy2()
{
	string json = Jsonstr("D:\\temp\\json2.txt");
	JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
	JsonClass2 list = js.Deserialize(json);    //将json数据转化为对象类型并赋值给list
	string result = list.result;    
	string res_info = list.res_info;
	string order_num = list.queryorder_info.order_num;
	string orderdetail = list.queryorder_info.orderdetail;
}

json数据中还包含数组对象集合

示例json字符串

{"result":"0","res_info":"ok","queryorder_info":
{"order_num":"5","orderdetail":[
{"Description":"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","Name":"有间客栈","ID":"1000000005201308280002999652","PayAmt":"228","PayType":"0","RommsCnt":"1","SPTransID":"65202157","Address":"4"},
{"Description":"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","Name":"七天酒店","ID":"1000000005201308280002999413","PayAmt":"1140","PayType":"0","RommsCnt":"1","SPTransID":"65197226","Address":"4"}]}}

构造对象

public struct JsonClass3
{
	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;//数组处理       
};
public struct orderdetail
{
	public string Description { 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 Name { get; set; }
	public string ID { get; set; }
	public string PayAmt { get; set; }
	public string PayType { get; set; }
	public string RommsCnt { get; set; }
	public string SPTransID { get; set; }
	public string Address { get; set; }
};

转换对象,,将json字符放在json3.txt文本中

public static void JsonMy3()
{
	string json = Jsonstr("D:\\temp\\json3.txt");
	JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
	JsonClass3 list = js.Deserialize(json);    //将json数据转化为对象类型并赋值给list
	string result = list.result;     
	string res_info = list.res_info;
	string order_num = list.queryorder_info.order_num;
	List orderdetail = list.queryorder_info.orderdetail;
	string Description = orderdetail[0].Description;
	string Name = orderdetail[0].Name;
	string ID = orderdetail[1].ID;
	string Address = orderdetail[2].Address;
}

以上就是关于json字符串转对象的全部,希望可以帮助你。