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