C # Serializes an object into a JSON string
public string getjsonstring () { list<product> products = new list<product> () { new Product () { Name= "Apple", price=5.5}, new product () {name= "orange", price=2.5}, new product () {name= "dried persimmon", price=16.00} }; ProductList productlist = new ProductList (); ProductList. GetProducts = 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;}
The main thing here is to use JavaScriptSerializer to implement serialization, so that we can convert the object into a JSON-formatted string, resulting in the following results:
How do I convert a JSON string into an object?
In actual development, it is often possible to encounter using JS to pass a JSON-formatted string into the background, if you can automatically convert the string to the desired object, it is more convenient to traverse or other operations. How is that specific?
public static list<t> jsonstringtolist<t> (This string jsonstr) {JavaScriptSerializer serializer = new J Avascriptserializer (); List<t> Objs = serializer.deserialize<list<t>> (JSONSTR); return OBJS; public static T deserialize<t> (string json) {T obj = activator.createinstance<t> (); using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (JSON))) {DataContractJsonSerializer serial Izer = new DataContractJsonSerializer (obj. GetType ()); Return (T) serializer. ReadObject (MS); }} string jsonstr = "[{Name: ' Apple ', Price:5.5},{name: ' Orange ', price:2.5},{name: ' Persimmon ', price:16}]"; list<product> products = new list<product> (); Products = jsonstringtolist<product> (JSONSTR); foreach (var item in products) {Response.Write (item. Name + ":" + Item. Price + "<br/>"); public class Product {public string Name {get; set;} Public double price {get; set;} }
In the above example, it is convenient to convert the JSON string into a list object, the operation of the time is more convenient ~
------------------------------------------------------------------------------------------------------------
C # Serializes an object into a JSON string
Using system.web.script;using System.Web.Script.Serialization; JavaScriptSerializer serializer = new JavaScriptSerializer (); foreach (DataRow dr in Dt. Rows) { dictionary<string, string> data = new dictionary<string, string> (); Data. ADD ("Dev_no", dr["Dev_no"]. ToString ()); Data. ADD ("Dev_name", dr["Dev_name"]. ToString ()); Data. ADD ("Ol_phone", dr["Ol_phone"]. ToString ()); Data. ADD ("Ol_ip", dr["Ol_ip"]. ToString ()); Data. ADD ("Ol_status", dr["Ol_status"]. ToString ()); List. ADD (data); } Response.Write (Serializer. Serialize (list));
The output is:
[{"Dev_no": "00066666", "Dev_name": "Test", "Ol_phone": "13766668888", "ol_ip": "10.196.209.174", "Ol_status": "2"},{" Dev_no ":" 000f0000 "," Dev_name ":" TEST_HZLKHQ "," Ol_phone ":" 13066668888 "," ol_ip ":" 192.168.1.198 "," Ol_status ":" 2 "}]
How do I convert a JSON string into an object?
public class Model {public string Dev_no {get; set;} public string Dev_name {get; set;} public string Ol_phone {get; set;} public string Ol_ip {get; set;} public int Ol_status {get; set;} }
String requestmes= "[{\" dev_no\ ": \" 00066666\ ", \" dev_name\ ": \" test\ ", \" ol_phone\ ": \" 13766668888\ ", \" ol_ip\ ": \" 10.196.209.174\ ", \" ol_status\ ": \" 2\ "},{\" dev_no\ ": \" 000f0000\ ", \" dev_name\ ": \" test_hzlkhq\ ", \" ol_phone\ ": \" 13066668888\ ", \" ol_ip\ ": \" 192.168.1.198\ ", \" ol_status\ ": \" 2\ "}]"; JavaScriptSerializer serializer = new JavaScriptSerializer (); var Json = serializer. Deserialize<list<model>> (Requestmes);
second use of datacontractjsonserializer, applicable to the common platform
You need to add references: System.ServiceModel.Web and System.Runtime.Serialization, and then use using:
Using system.runtime.serialization.json;using system.runtime.serialization;//--------------------------Public Class Model {public string Dev_no {get; set;} public string Dev_name {get; set;} public string Ol_phone {get; set;} public string Ol_ip {get; set;} public int Ol_status {get; set;} }
public class Test {public static DataTable GetData () { var a = "{\" dev_no\ ": \" 00066666\ ", \" dev_name\ ": \" test\ ", \" ol_phone\ ": \" 13766668888\ ", \" ol_ip\ ": \" 10.196.209.174\ ", \" ol_status\ ": \" 2\ "}"; var serializer = new DataContractJsonSerializer (typeof (List<dtumodel>)); var mstream = new MemoryStream (Encoding.UTF8.GetBytes (a.tostring ())); list<dtumodel> list = (list<dtumodel>) serializer. ReadObject (mstream); DataTable dt = new DataTable (); return dt; } }
Turn: Treat yourself, cherish today, grace others, enjoy life C # JSON string serialization and deserialization (GO)
Ext.: http://blog.csdn.net/cjw13860421089/article/details/26374969
C # JSON string serialization and deserialization