Global Reference
Using Newtonsoft. Json;
Using Newtonsoft. Json. Converters;
// Deserializes A Json string into an object
Target object = JavaScriptConvert. DeserializeObject (JSON string, typeof (target object ));
// Serialize the target object into a Json string
StringJson string = JavaScriptConvert. SerializeObject (target object );
1. Reference Newtonsoft. Json. dll;
2. Add references to the project;
Serialization and deserialization are simple examples in. net projects.
Copy codeThe Code is as follows:
Productproduct = newProduct ();
Product. Name = "Apple ";
Product. Expiry = newDateTime (2008, 12, 28 );
Product. Price = 3.99 M;
Product. Sizes = newstring [] {"Small", "Medium", "Large "};
Stringoutput = JavaScriptConvert. SerializeObject (product );
Note:
1. serialize an object
1. You must add the [DataContract] flag to the class name;
2. Add the [DataMember] tag to the class attribute;
2. If a field does not need to be serialized, you can add the [JsonIgnore] mark to the field.
There are multiple methods for date serialization in the Newtonsoft class library. You can add the corresponding tag to the DataTime member of the class, so that the serialization and deserialization will follow the specified method,
In this example, the CreateDate attribute of the User class (the following class) is added to [JsonConverter (typeof (IsoDateTimeConverter)], [JsonConverter (typeof (JavaScriptDateTimeConverter)] is added to the Birthday attribute. The final expressions of these attributes are different from those of serialization.
Copy codeThe Code is as follows:
[DataContract]
Public class User
{
/// <Summary>
/// No.
/// </Summary>
[DataMember]
Public int UserId {get; set ;}
/// <Summary>
/// 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] // This field is not serialized when using JavaScriptSerializer for serialization.
[IgnoreDataMember] // This field is not serialized when DataContractJsonSerializer is used for serialization.
[JsonIgnore] // This field is not serialized when JsonConvert is used for serialization.
Public int Salary {get; set ;}
/// <Summary>
/// Permission level
/// </Summary>
[DataMember]
Public Priority {get; set ;}
Public User ()
{
Urls = new List <string> ();
}
}
Others:
Newtonsoft. json. the JsonConvert class is a non-Microsoft open-source free class library for JSON serialization and deserialization (download URL: http://www.codeplex.com/json/) that provides more flexible serialization and deserialization control, and if your development environment is. NET Framework3.5 and later versions, you can use Linq to JSON, so that you do not have to parse a large segment of data one by one, you can use Linq to JSON to parse the part you care about, which is very convenient.
Refer:
Http://www.cnblogs.com/gghxh/archive/2008/01/11/1035482.html
Http://blog.csdn.net/zhoufoxcn/article/details/6254657