JSON Learning Summary

Source: Internet
Author: User
Tags string format string to json

The need to use JSON in recent days is more urgent, so I learned a bit about JSON. This article should only be taken as a note in case of forgetting.

This article is mainly divided into JS and Java summary:

The JSON format is introduced first:

The writing format for JSON data is: name/value pairs.

    * * *: The nested format of JSON can be understood more easily.

The JSON value can be:

      • Number (integer or floating point)
      • String (in double quotes)
      • Logical value (TRUE or FALSE)
      • Array (in square brackets) * * *
      • Object (in curly braces) * * *
      • Null

  

1. JS

The full name of JSON is: JavaScript object Notation (JAvaScript Object Notation).

JSON is visible at the beginning of the service for JS, but in the later development, gradually became independent of the various languages of the data transmission format.

The use of JS is mainly divided into:

1> Constructing JSON data units

A.json belongs to the JS object notation, its initialization can be completely consistent with the object, that is, when the JS object is initialized, this object is a JSON-formatted data unit .

The B.eval () function uses a JavaScript compiler to parse JSON text and then generate JavaScript objects. You must enclose the text in parentheses in order to avoid syntax errors:

var obj = eval ("(" + txt + ")");

C. Json.parse (); You can convert a string to JSON-formatted data, or you can interpret it as a JS object.

      

2>json object resolves to a string

A. Json.stringify (); You can convert a JSON data unit into a string format.

     * The Json.parse () and Json.stringify () methods are both browser-enabled methods, but ie8-are not supported and require reference json.js compatibility.

2. JAVA

JSON usage in Java uses primarily the Json-lib.jar package, which encapsulates some of the available JSON methods.

The main classes used are: Jsonarray, Jsonobject

    

Jsonarray implements the list interface;

Josnobject realizes the map interface;

    * It can be directly assigned to list or map using the above transformation;

Both can use T.fromobject (string) to implement the string into the corresponding class;

The Jsonobject class can be obtained in jsonarray.getjsonobject ();

Jsonobject can use the various get methods to get the element values in them.

JSON Learning Summary

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.