Week 7 Blog - JSON Parsing (Compared with XML)

1 What is JSON?

JSONwhat is it? Wikipedia explains it this way.

JSON(JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON is in a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data interchange language. It is easy for humans to read and write, but also easy for machines to parse and generate (generally used to increase network transmission rates).

In order not to be out of date, we have to learn XML and JSON, but at the same time they are easy to learn. Also join our JSON army:QQ群:259217951

XML is nice and powerful, but there's another era that's been making waves lately, and that's JSON. Now the halo of JSON has gradually surpassed XML, and the data interfaces provided by major websites are generally JSON. Now let's learn about JSON.

2 JSON format

JSON is built on two structures:

  1. A collection of name/value pairs. In different languages, it is understood as object, record, struct, dictionary, hash table, keyed list, or associative array array).
  2. An ordered list of values. In most languages, it is understood as an array (array), vector (vector), list (list) or sequence (sequence).

JSON has these forms:

  • An object is an unordered collection of "'name/value' pairs". An object begins with "{" (left bracket) and ends with "}" (right bracket). Each 'name' is followed by a ':' (colon); 'name/value' pairs are separated by a ',' (comma).

JSON Object

  • An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Use "," (comma) to separate the values.
  • JSON Array
    • The value can be a string enclosed in double quotes, a number, true, false, null, an object, or an array. These structures can be nested.

    JSON Value

    • A string is a sequence of 0 or more Unicode characters enclosed in double quotes (""), which can be escaped with a backslash ('\'). A character can be represented as a single character string.

    JSON String

    • Numbers are similar to numbers in C or Java, except for octal and hexadecimal numbers that are not used.

    JSON Number

 

 

3 How to parse JSON?

All Android JSON related classes are under the org.json package.

Includes JSONObject, JSONArray, JSONStringer, JSONTokener, JSONWriter, JSONException.

<1>. Common methods

At present, there are two methods for JSON parsing, namely get and opt methods, which can use JSON

So what is the difference between using the get method and using the opt method?

JsonObject method, opt and get are recommended to use opt method, because get method will throw an exception directly if its content is empty. However, JsonArray.opt(index) will have out-of-bounds problems that need special attention.

 

 4 JSON vs. XML

JSON and XML are like the Dragon Slayer Sword and Yitian Sword in the martial arts world, so which one is stronger and which one is weaker?

XML has long been the leader in the data transmission industry, and JSON, as a rising star, has already challenged its leaders.

Then let them do PK:

<1>. The difference between JSON and XML

  • no closing tag
  • Shorter
  • Read and write faster
  • Ability to parse using the built-in JavaScript eval() method
  • use an array
  • Do not use reserved words

In conclusion: JSON is smaller, faster, and easier to parse than XML.

<2>. The difference between XML and JSON:

The main components of XML:

 
  1. XML is element , attribute and element content .

The main components of JSON:

 
  1. JSONobjectarraystringnumberboolean(true/false)和null

To represent an object (referring to a collection of name-value pairs) in XML, element may be used initially as an object, and each key-value pair is represented by an attribute:

 
  1. <studentname="soゝso"age="27"/>

But if a value is also an object, then it cannot be used as an attribute:

 
  1. <studentname="soゝso"age="27">
  2. <address>
  3. <country>中国</country>
  4. <province> Beijing </province>
  5. <city> Chaoyang District </city>
  6. <district> Room 1906, Block A, Ocean International Center, East Fourth Ring Road, Chaoyang District, Beijing </district>
  7. </address>
  8. </student>

So, when to use element and when to use attribute is already a problem.

Since JSON has the type of object, it can be mapped naturally, without considering the above problems, and the following format is naturally obtained.

 
  1. {
  2. "name":"John",
  3. "age":10,
  4. "address":{
  5. "country":"中国",
  6. "province":"北京市",
  7. "city" : "Chaoyang District" ,
  8. "district" : "Room 1906, Block A, Ocean International Center, East Fourth Ring Road, Chaoyang District, Beijing" ,
  9. }
  10. }

One More Thing…

XML needs to choose how to handle element content line breaks, while JSON string does not need to make this choice.

XML has only text and no preset number format, while JSON has a clear number format, which is also safe on locale.

There is no big problem with XML mapping arrays, that is, the tags of the array elements are repetitive and redundant. JSON is easier to read.

JSON's true/false/null can also be easily unified to the corresponding semantics of general programming languages.

XML documents can be attached with DTD, Schema, and a bunch of specifications such as XPath. Using custom XML elements or attributes can easily attach various constraints and associated additional information to the data. From the perspective of data expression capabilities, XML is stronger than Json, but many scenarios do not require such complex and heavyweight things, and the lightweight and flexible Json is very popular.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613008&siteId=291194637