JSON. Net learning notes (1)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.