JSON

json的定义:

JSON(Javascript Object Notation) is a lightweight data-interchange format .是一种轻量级的数据交换格式。 

It is easy for humans to read and write. It is easy for machines to parse and generate.

它易于人们去读和写和易于机器去解析和生成.

It is based on a subset of the JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999.

它是基于javascript编程语言的一个子集。

JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON是一个完全独立的文本格式语言,但是使用了类似于C系家族的一些约定,包括C,C++,C#,JAVA JAVASCRIPT,PYTHON以及其他的很多。这些特性使得JSON称为理想的数据交换语言。

JSON的构建方式:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • 键值对的集合,在别的语言中,他们被实现为一个object,record,struts,字典,hash表。key列表挥着向管理啊的数组。
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
  • 一个有序的列表值,在大部分的语言中,这被实现为一个数组,集合,列表或者队列。

采用形式:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon)and the name/value pairs are separated by , (comma).

一个对象是一个无序的键值对的集合。一个对象以一个{开始}结束。每一个name有一个冒号跟随,每一个键值对用逗号分开。

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

一个数组是一个有序集合的值,一个数组以一个[开始,]结束,以逗号分隔。

value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

一个值可以使用双引号引用的字符串,数字或boolean或者对象和数字。这些结构是可以嵌套的。

string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

一个字符串是0个或多个Unicode字符,由双引号包裹,使用转义字符。一个字符是被表示为一个单独的字符串。字符串非常类似于C或Java中的字符串。

number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

数字非常类似于C或Java中的数字,但是八进制和十六进制的除外。

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

空格可以插入到任何一对标志中。除了一些编码细节,这基本完全描述了这门语言。

 特性:

JSON 不是一个文本格式

JSON 不是一种标记语言

JSON不是一种通用的可序列号的语言。

JSON和XML的对比:

<users>
<user>
<username>sanglp</username>
<password>122</password>
<age>18</age>
<address>Shanghai</address>
</user>
<user>
<username>sanglp</username>
<password>122</password>
<age>18</age>
<address>Shanghai</address>
</user>
</users>

[{username:'sanglp',password:'122',age:18,address:shanghai},{username:'sanglp',password:'122',age:18,address:shanghai}]

 练习:

 1 <section>
 2 <title>Book-Signing Event</title>
 3 <signing>
 4 <author title ="Mr" name ="Vikram Seth"/>
 5 <book title="A Suitable Boy" price = "$22.95"/>
 6 </signing>
 7 <signing>
 8 <author title ="Mr" name ="Vikram Seth"/>
 9 <book title="A Suitable Boy" price = "$22.95"/>
10 </signing>
11 </section>
<script type="text/javascript">
var s={
  "section":
  {
    "title":"Book-Signing EventBook-Signing Event",
    "siging":[{
      "autor":{"title":"Mr" , "name":"Vickram Seth"},
      "book":{"title":"A Suitable Boy" , "Price" :"$22.95"}
    },
    {
      "autor":{"title":"Mr" , "name":"Vickram Seth"},
      "book":{"title":"A Suitable Boy" , "Price" :"$22.95"}
    }
    
    ]
  }
};
</script>

 示例:

 1 String jsonContent = "{'hello':'world','age':123}";//构建json字符串
 2     JSONObject jsonObject = new JSONObject(jsonContent);//json字符串转换为json对象
 3     String str1 = jsonObject.getString("hello");//获取值
 4     Integer str2 = jsonObject.getInt("age");
 5     System.out.println(str1+"====="+str2);
 6     System.out.println("-------------------");
 7     jsonContent = "[{'hello':'world','age':123,'abc':'xyz'},{'hello':'world','age':123,'abc':'xyz111'}]";//构建json数组字符串以[开始以]结束
 8     JSONArray jsonArray = new JSONArray(jsonContent);//转换为json数组
 9     for(int i=0;i<jsonArray.length();i++){//遍历json数组
10         JSONObject jo = jsonArray.getJSONObject(i);//通过getJSONObject方法获取jsonObject对象
11         String hello = jo.getString("hello");
12         int age  = jo.getInt("age");
13         String abc = jo.getString("abc");
14         System.out.println(hello+"====="+age+"======="+abc);
15     }
16 
17 
18 world=====123
19 -------------------
20 world=====123=======xyz
21 world=====123=======xyz111    
原文地址:https://www.cnblogs.com/dream-to-pku/p/6715070.html