Newtonsoft. JSON is an open source JSON. Net Library.
: Http://json.codeplex.com/releases/view/50552. The current version is release 8.
Download fromSource codeObtain newtonsoft. JSON. netw.dll and add it to your project.
Using newtonsoft. JSON;
Definition class:
Public class message
{
Public String address {Get; set ;}
[Jsonproperty (typenamehandling = typenamehandling. All)]
Public Object Body {Get; set ;}
}
Public class searchdetails
{
Public String query {Get; set ;}
Public String language {Get; set ;}
}
Test:
Message message = new message
{
Address = "http://google.com /",
Body = new searchdetails {query = "JSON. Net", language = "En-us "}
};
String jsonmsg = jsonconvert. serializeobject (message, formatting. indented); // indented indicates that the result is displayed in indentation.
System. Diagnostics. Debug. Write (jsonmsg );
Message deserialized = jsonconvert. deserializeobject <message> (jsonmsg );
Searchdetails = (searchdetails) deserialized. Body;
Response. Write (searchdetails. query + "," + searchdetails. Language + "<br/> ");
Debug output result format:
{
"Address": "http://google.com /",
"Body ":{
"$ Type": "testjsonserialization. searchdetails, testjsonserialization ",
"Query": "JSON. Net ",
"Language": "En-us"
}
}
Note: 1. The jsonproperty tag is used to control the serialization of a field or attribute as a JSON object.
2. typenamehandling is used to specify the type name for JSON serialization. It has several enumerated values:
Member |
Description |
none |
do not include. net Type name when serializing types. |
objects |
include. net Type name when serializing into a JSON object structure. |
arrays |
include. net Type name when serializing into a JSON array structure. |
auto |
include. net Type name when the type of the object being serialized is not the same as its declared type. |
All |
Always include the. NET type name when serializing. |