This article mainly describes the C # implementation of JSON and object conversion between the function, in conjunction with the case of the form of a more detailed analysis of C # Implementation of the object and JSON conversion between the operation skills, the need for friends can refer to the next
The examples in this article describe the ability of C # to convert JSON and objects to each other. Share to everyone for your reference, as follows:
1. The first is to declare the user information object, DataContract decorated class, the expression can be parsed into json,datamember decorated property, order represents the sequence of resolution, and lover is an array list, indicating the number of girlfriends
Address represents a shipping location, Dailyrecord represents a Daily Record
using system;using system.collections.generic;using system.linq;using System.Text;using System.runtime.serialization;namespace functiontest.model{[DataContract] public class UserInfo {[DataMember (Order =0)] public string UserName {get; set;} [DataMember (Order = 1)] public int Age {get; set;} [DataMember (Order = 2)] public int Gender {get; set;} [DataMember (Order =3)] public list<string> Lover {get; set;} [DataMember (Order = 4)] public contactaddress Address {get; set;} [DataMember (Order = 5)] public dictionary<string, string> Dailyrecord {get; set; }} [DataContract] public class Contactaddress {[DataMember (Order =0)] public string province {get; set;} [DataMember (Order = 1)] public string City {get; set;} [DataMember (Order = 2)] public string Country {get; set;} [DataMember (Order = 3)] public string Details {get; set;} }}
2.JSON Helper Class Core code
<summary>///JSON converted to object///</summary>///<typeparam name= "T" ></typeparam>///<param Name= "Jsontext" ></param>///<returns></returns>public static T jsontoobject<t> (string Jsontext) {DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (T)); MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (Jsontext)); T obj = (t) s.readobject (MS); Ms. Dispose (); return obj;} <summary>///object converted to json///</summary>///<typeparam name= "T" ></typeparam>///<param Name= "obj" ></param>///<returns></returns>public static string objecttojson<t> (T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (T)); string result = String. Empty; using (MemoryStream ms = new MemoryStream ()) {serializer. WriteObject (MS, obj); Ms. Position = 0; using (StreamReader read = new StreamReader (ms)) {result = read. ReadToEnd (); }} return Result;}
3. Call
1. Object-->jsonuserinfo info = new userinfo{Age = ten, Gender = 1, UserName = "Andy Lau", Lover = new list< string> {"Beauty 1", "Beauty 2", "Beauty 3"}, Address = new contactaddress { province = "Hunan province", City = "Changsha", Cou ntry = "Wangcheng County", Details = "Place not found in a nook" }, Dailyrecord = new dictionary<string, string> {"Monday", "Eat" }, {"Tuesday", "Wash Clothes"}, {"Wednesday", "Good Thing"}}};string JSON = objecttojson<userinfo> (info);
4. Results after deserialization
The code is as follows:
{"UserName": "Andy Lau", "age": Ten, "Gender": 1, "Lover": ["Beauty 1", "Beauty 2", "Beauty 3"], "Address": {"Province": "Hunan Province", "City": "Changsha", " Country ":" Wangcheng County "," Details ":" A place that cannot be found in a nook "}," Dailyrecord ": [{" Key ":" Monday "," value ":" Eat "},{" key ":" Tuesday "," value ":" Laundry "}, {"Key": "Wednesday", "Value": "Good Thing"}]}