JSON using notes

Source: Internet
Author: User

Open Source Projects
    • Org.json This project is relatively weak, but many projects are quoted
    • Gson This has been used before, and later Fastjson out, see others use more, also use Fastjson
    • Fastjson
      • Online said speed is relatively fast, not sensitive to speed, but its interface is more concise than Gson.
      • Customization, can be resolved by @jsonfield the JSON name contains underscores, casing and other non-canonical issues
      • You can even customize which field is output
Specification

began to refer to the JSON specification, this document is relatively chaotic, the following re-organized under. And the official documents are clear.

1.JSON syntax

The JSON text is a sequence of tokens. This set of tokens contains six constructed characters, strings, numbers, and three name names.
The following are six constructed characters:

symbols meaning Chinese name
Begin-array [ Left bracket
Begin-object { Left Curly Brace
End-array ] Right Bracket
End-object } Closing curly Braces
Name-separator : Colon
Value-separator , Comma

Meaningless whitespace characters are allowed before or after these six constructed characters.

ASCII code name
0x20 Space character
0x09 Horizontal tab
0x0A Line break
0x0D Carriage return character
2. Value

JSON must (must) be an object, array, number, or string, or name name:
Value = false/null/true /object/array/number/string

2.1. Name Name
Only three false null True
The name name must (must) be lowercase, and no other name name is allowed.
2.2. Objects
The object structure is represented as: a pair of curly braces surrounds 0 or more name/value pairs (or members).

2.3. Arrays
The array structure is represented as: square brackets surround 0 or more values (or elements). The elements are separated by commas.

2.4. Digital
The representation of numbers is similar to most other programming languages. Points:

-no eight binary and hexadecimal forms are allowed.
-The front with 0 is also forbidden.
-numeric values cannot be represented as sequences of Arabic numerals (such as infinity and Nan are not allowed).

2.5. String

-String begins and ends with quotation marks.
-All Unicode characters can be placed in quotation marks, except for escape characters: quotation marks, backslash \, control (u+0000-u+001f), which can be expressed in Unicode encoding: "\u005c".
-Another way, you can use two escape character sequences to represent some of the commonly used characters. So a string that contains only a backslash character can be more succinctly represented as "\".

Fastjson main functions

Detailed official documents
Main interface

 Public Static FinalObjectParse(String text);//JSON text parse to Jsonobject or Jsonarray Public Static FinalJsonobjectparseobject(String text);//parse the JSON text into Jsonobject Public Static FinalTparseobject(String text, Class clazz);//JSON text parse to JavaBean Public Static FinalJsonarrayParsearray(String text);//Parse the JSON text into Jsonarray Public Static FinalListParsearray(String text, Class clazz);//Parse the JSON text into a JavaBean collection Public Static FinalStringtojsonstring(Object object);//Serialize JavaBean to JSON text Public Static FinalStringtojsonstring(Object object,BooleanPrettyformat);//Serialize JavaBean to formatted JSON text Public Static FinalObjectToJSON(Object javaobject);//6 convert JavaBean to Jsonobject or Jsonarray. 

Several common functions:
1. Non-canonical name processing
@jsonfield can be configured in a field or Getter/setter method such as @jsonfield (name= "_id"), detailed reference to the official documentation

2. Date processing
The API that processes the date, serialized Json.tojsonstringwithdateformat (date, "Yyyy-mm-dd HH:mm:ss. SSS ")

Deserialization can automatically recognize the following date formats:

    • ISO-8601 Date format
    • Yyyy-mm-dd
    • YYYY-MM-DD HH:mm:ss
    • Yyyy-mm-dd HH:mm:ss. Sss
    • Millisecond number
    • Millisecond numeric string
    • . NET JSON date format
    • New Date (198293238)

3. Define a number of fields to be serialized
Using Simplepropertyprefilter filtering properties, refer to

  new"name1""name2");  JSON.toJSONString(vo, filter);

4.JSON formatted output
such as json.tojsonstring (Xx,serializerfeature.prettyformat);

Other precautions
    1. If you include an inner class, you need to add the static class modifier, or the deserialization will fail .
    2. In addition, when referencing different frameworks or libraries, different JSON frameworks are used, such as IO framework is Org.json, my business code has been used Fastjson, in order to merge with the business code, first jsonobject to ToString, and then Tojson, feel good silly ~~

JSON using notes

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.