Json.net Basics

Source: Internet
Author: User

Introduction:

JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript programming language, standard ECMA-262 3rd edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:

  • A collection of name/value pairs ). In different languages, it is understoodObject), Record, structure (struct), Dictionary, hash table, keyed list, or associative array ).
  • An ordered list of values ). In most languages, it is understood as an array ).

Advantages:

When reading data, you can directly transmit JSON objects from the backend. For example, the data source of the Grid Control of ext can be JSON, or XML. However, compared with JSON, data is simpler and the data size transmitted is smaller than XML. When using these applications, we can use json.net to easily convert objects in. Net into JSON strings. Then it is passed to the previous section for calling.

 

Example:

Javascriptconvert. serializeobject: converts an object to a josn string
 

 

Productproduct = newproduct ();

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

Newtonsoft. JSON. javascriptconvert. deserializeobject: deserializes the object based on the JSON string.

 

    ProductdeserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output,typeof(Product));
The output is a JSON string, and the product is a class object to be deserialized.

Through these two common methods, you can easily get the object from the JSON string and get the JSON string from the object.

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.