C#實(shí)現(xiàn)JSON字符串序列化與反序列化的方法
C#將對(duì)象序列化成JSON字符串
public string GetJsonString() { List<Product> products = new List<Product>(){ new Product(){Name="蘋果",Price=5}, new Product(){Name="橘子",Price=5}, new Product(){Name="干柿子",Price=00} }; ProductList productlist = new ProductList(); productlistGetProducts = products; return new JavaScriptSerializer()Serialize(productlist)); } public class Product { public string Name { get; set; } public double Price { get; set; } } public class ProductList { public List<Product> GetProducts { get; set; } }
這里主要是使用JavaScriptSerializer來(lái)實(shí)現(xiàn)序列化操作,這樣我們就可以把對(duì)象轉(zhuǎn)換成Json格式的字符串,生成的結(jié)果如下:
{"GetProducts":[{"Name":"蘋果","Price":5},{"Name":"橘子","Price":5},{"Name":"柿子","Price":16}]}
如何將Json字符串轉(zhuǎn)換成對(duì)象使用呢?
在實(shí)際開(kāi)發(fā)中,經(jīng)常有可能遇到用JS傳遞一個(gè)Json格式的字符串到后臺(tái)使用,如果能自動(dòng)將字符串轉(zhuǎn)換成想要的對(duì)象,那進(jìn)行遍歷或其他操作時(shí),就方便多了。那具體是如何實(shí)現(xiàn)的呢?
public static List<T> JSONStringToList<T>(this string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = SerializerDeserialize<List<T>>(JsonStr); return objs; } public static T Deserialize<T>(string json) { T obj = ActivatorCreateInstance<T>(); using (MemoryStream ms = new MemoryStream(EncodingUTFGetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(objGetType()); return (T)serializerReadObject(ms); } } string JsonStr = "[{Name:'蘋果',Price:5},{Name:'橘子',Price:5},{Name:'柿子',Price:16}]"; List<Product> products = new List<Product>(); products = JSONStringToList<Product>(JsonStr); foreach (var item in products) { ResponseWrite(itemName + ":" + itemPrice + "<br />"); } public class Product { public string Name { get; set; } public double Price { get; set; } }
在上面的例子中,可以很方便的將Json字符串轉(zhuǎn)換成List對(duì)象,操作的時(shí)候就方便多了~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#使用Json.Net對(duì)JSON與對(duì)象的序列化與反序列化
- C#對(duì)Json進(jìn)行序列化和反序列化
- C# Newtonsoft.Json 解析多嵌套json 進(jìn)行反序列化的實(shí)例
- C#使用Json.Net進(jìn)行序列化和反序列化及定制化
- C#中Json反序列化的實(shí)現(xiàn)方法
- C#中實(shí)現(xiàn)Json序列化與反序列化的幾種方式
- C#實(shí)現(xiàn)的json序列化和反序列化代碼實(shí)例
- C#實(shí)現(xiàn)json的序列化和反序列化實(shí)例代碼
- C#對(duì)JSON與對(duì)象的序列化與反序列化
相關(guān)文章
WPF實(shí)現(xiàn)環(huán)(圓)形菜單的示例代碼
這篇文章主要介紹了如何利用WPF繪制一個(gè)簡(jiǎn)單的環(huán)形菜單,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07c# List find()方法返回值的問(wèn)題說(shuō)明(返回結(jié)果為對(duì)象的指針)
本篇文章主要介紹了c#中List find()方法返回值的問(wèn)題說(shuō)明(返回結(jié)果為對(duì)象的指針) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(4)
這篇文章主要介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì),學(xué)習(xí)內(nèi)容是總結(jié)銷售信息的保存以及加載銷售信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼
這篇文章主要為大家詳細(xì)介紹了Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07詳解如何利用C#實(shí)現(xiàn)設(shè)置系統(tǒng)時(shí)間
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置系統(tǒng)時(shí)間功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12