Original: Json.NET Use tips
1. Serialization-related Tips
Ignore some properties
Sometimes we have the need to serialize only a subset of the attributes in the entity class, and we can use the declaration to ignore some of the attributes that we do not need to serialize, in two ways to achieve this goal:
First, consider using attributes to JsonIgnore
decorate properties that do not need to be serialized, as follows:
public class Employee{public Guid Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public decimal Salary { get; set; } public string Phone { get; set; }[JsonIgnore] public DateTime HireDate { get; set; }}
To run the program:
var employeeBean = new EmployeeBean(){Id = Guid.NewGuid(),Name = "gyzhao",Email = "[email protected]",Salary = 10000,Phone = "13912390987",HireDate = new DateTime(2012, 2, 1)};var jsonString = JsonConvert.SerializeObject(employeeBean, Formatting.Indented);//输出://{// "Id": "69a406ad-902c-45d3-8ba7-89a09779ed52",// "Name": "gyzhao",// "Email": "[email protected]",// "Salary": 10000.0,// "Phone": "13912390987"//}
If you need to serialize a class with a lot of attributes, and you need to use a small part of it, it would be tedious to use the above method (because there are too many properties to ignore), consider using attributes to DataContract
decorate the serialized class, using DataMember
the Attribute decorations require a property to be serialized, and no other attribute property is automatically ignored. As shown below:
[DataContract]public class EmployeeBean{[DataMember]public Guid Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Email { get; set; } [DataMember] public decimal Salary { get; set; } public string Phone { get; set; } public DateTime? HireDate { get; set; }}
To run the program:
var employeeBean = new EmployeeBean(){Id = Guid.NewGuid(),Name = "gyzhao",Email = "[email protected]",Salary = 10000,Phone = "13912390987",HireDate = new DateTime(2012, 2, 1)};var jsonString = JsonConvert.SerializeObject(employeeBean, Formatting.Indented);//输出://{// "Id": "69a406ad-902c-45d3-8ba7-89a09779ed52",// "Name": "gyzhao",// "Email": "[email protected]",// "Salary": 10000.0//}
DataContract
Attributes and DataMember
attributes are subordinate to: System.Runtime.Serialization
namespaces.
Workarounds for circular Reference exceptions when serializing objects
When serializing an object, if the object has a collection property, and the type of the collection is the object itself, the default serialization method will report a circular reference to the exception, and if serialization is required, simply declare the following attribute:
JsonConvert.SerializeObject(result,new JsonSerializerSettings{ReferenceLoopHandling=ReferenceLoopHandling.Serialize})
2. Deserialization-related Tips
2.1 Using an anonymous type as a deserialized entity
var jsonString = @"{ 'Id': '69a406ad-902c-45d3-8ba7-89a09779ed52', 'Name': 'gyzhao', 'Salary': 10000.0, 'HireDate': '2012-02-01T00:00:00' }";var employee = new { Name = default(string), Salary = default(decimal), HireDate = default(DateTime), Id = default(Guid) };var employeeBean = JsonConvert.DeserializeAnonymousType(jsonString, employee);
3. Create JSON
Imperative to create a JSON object var array = new Jarray (); var text = new Jvalue ("Manual text"); var date = new Jvalue (DateTime.Now); Array. ADD (text); Array. ADD (date); Console.WriteLine (Array. ToString ()); Use declarative Syntax var RSS = new Jobject (New Jproperty ("channel", New Jobject ( New Jproperty ("title", "James nexton-king"), New Jproperty ("link", "http://james.newtonking.com"), New Jproperty ("description", "James Newton-kin ' s blog."), New Jproperty ("Item", "BB")) )); Console.WriteLine (RSS. ToString ()); Create JSON jobject with an anonymous object o = Jobject.fromobject (new {channel = new {T Itle = "James newton-king", link = "http://james.netwoing.com", item = new LIST<STRING> ;() {"A", "B", "C", "D", "E"}}); Console.WriteLine (O.tostring ());
Reference & Further Reading
Http://www.newtonsoft.com/json
Json.NET Tips for use