Introduction to the methods of converting reading and writing using Newtonsoft.Json in.NET

  • 2020-05-17 05:07:44
  • OfStack

Global references
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

// deserializes the Json string into an object
Target object = JavaScriptConvert.DeserializeObject (JSON string, typeof(target object));
// serializes the target object to an Json string
stringJson string = JavaScriptConvert.SerializeObject (target object);
1. Newtonsoft. Json. dll;
2. Add references in the project;
Serialization and deserialization in the.net project, simple example

 
Productproduct = newProduct(); 
product.Name = "Apple"; 
product.Expiry = newDateTime(2008, 12, 28); 
product.Price = 3.99M; 
product.Sizes = newstring[] { "Small", "Medium", "Large"}; 
stringoutput = JavaScriptConvert.SerializeObject(product); 


Note:
1. If you need to serialize entities
1. The class name must be marked with [DataContract];
2. Add [DataMember] tag to class attributes;
2. If you have a field that doesn't need to be serialized, you can add a [JsonIgnore] flag to it
There are several ways to serialize a date in the Newtonsoft library. You can add the corresponding tag to the DataTime member of the class, so that when serializing and deserializing, the date will be serialized in the specified way.
In this case, the CreateDate property of the User class (as follows) adds the property [JsonConverter(typeof(IsoDateTimeConverter))], while the Birthday property adds the property [JsonConverter(typeof(JavaScriptDateTimeConverter))], and the serialization results show that they do not end up looking the same.
 
[DataContract] 
public class User 
{ 
/// <summary> 
///  Serial number  
/// </summary> 
[DataMember] 
public int UserId { get; set; } 
/// <summary> 
///  The user name  
/// </summary> 
[DataMember] 
public string UserName { get; set; } 
/// <summary> 
///  Creation time  
/// </summary> 
[DataMember] 
[JsonConverter(typeof(IsoDateTimeConverter))] 
public DateTime CreateDate { get; set; } 
/// <summary> 
///  birthday  
/// </summary> 
[DataMember] 
[JsonConverter(typeof(JavaScriptDateTimeConverter))] 
public DateTime Birthday { get; set; } 
/// <summary> 
///  related URL 
/// </summary> 
[DataMember] 
public List<string> Urls { get; set; } 
/// <summary> 
///  salary  
/// </summary> 
[ScriptIgnore]// use JavaScriptSerializer This field is not serialized when serialized  
[IgnoreDataMember]// use DataContractJsonSerializer This field is not serialized when serialized  
[JsonIgnore]// use JsonConvert This field is not serialized when serialized  
public int Salary { get; set; } 
/// <summary> 
///  The right level  
/// </summary> 
[DataMember] 
public Priority Priority { get; set; } 

public User() 
{ 
Urls = new List<string>(); 
} 
} 


Other:
The Newtonsoft.Json.JsonConvert class is an open source free JSON serialization and de-serialization library that is not provided by Microsoft (download: http: / / www. codeplex. com json /), it provides a more flexible serialization and deserialization control, and if you use the development environment is. NET Framework3. 5 and later, you can use Linq to JSON, like 1 to 1 of 11 parsing data don't have to, you can use Linq to JSON parsing out you care about that part of the can, is very convenient.

Reference:

http://www.cnblogs.com/gghxh/archive/2008/01/11/1035482.html
http://blog.csdn.net/zhoufoxcn/article/details/6254657


Related articles: