6.1 JSON background knowledge
6.1.1 JSON Introduction
JSON: JavaScript Object Notation (JavaScript Object Notation)
JSON syntax for storing and exchanging text messages. Similar to XML.
JSON is smaller than XML, faster, and easier to resolve.
Javascript use JSON syntax to describe data objects, but JSON is still independent of language and platform. JSON parser and JSON library supports many different programming languages. Currently a lot of dynamic (PHP, JSP, .NET) programming languages support JSON.
6.1.2 JSON syntax
JSON syntax rules
- Data in the name / value pairs
- Data separated by commas
- Save Object braces
- Save array square brackets
JSON name / value pairs
JSON data writing format is: name / value pairs.
Name / value pair comprises a field name (in double quotes), write behind a colon, then the value:
"Name": "zhangsan" |
It is easy to understand, this is equivalent to the JavaScript statement:
Name = "zhangsan" |
JSON value type
JSON values can be:
- String (in double quotes)
- Number (integer or floating point)
- Objects (in braces)
- An array (in square brackets)
- A logical value (true or false)
- Null
FIG value data types:
6.1.3 JSON data structure
JSON has two data structures: and an array of objects.
JSON object
The object is an unordered "name / value pairs" set (A collection of name / value pairs). Different languages, it is understood as an object (Object), the record (Record), structure (struct), dictionary (Dictionary), the hash table (hash table), with a list of keys (keyed list), or associative array (associative array).
An object to "{" (left bracket) starts, "}" (right parenthesis) ends. Each "name" followed by a ":" (colon); "" name / value pairs "used between", "(comma).
JSON object written in curly braces:
{ "name":"zhangsan" , "age":24 } |
It is also easy to understand, and this statement is equivalent JavaScript:
name = "zhangsan" age = 24 |
Referring to FIG follows:
JSON array
Ordered list of values (An ordered list of values). In most languages, it is understood as an array (array).
JSON array written in square brackets:
Array may comprise a plurality of objects:
{ "people": [ { "namr":"zhangsan" , "age":22 }, { "name":"lisi" , "age":24 }, { "name":"wangwu" , "age":27 } ] } |
In the above example, the object "employees" is an array comprising three objects. Each object represents a record of a person (name and surname) of.
Referring to FIG follows:
6.2 Java operating data in JSON
There are many online JAVA modes of operation JSON jar package, introduced one of the most common jar package here: json-lib.
6.2.1 Jar package download
Download: http://json-lib.sourceforge.net/
6.2.2 The basic method
Normal data transfer JSONObject
* 普通数据转 JSON
*/
public static void JsonTest1() {
JSONObject normalJson = new JSONObject();
normalJson.put("name", "zhangsan");
normalJson.put("sex", "男");
normalJson.put("age", new Integer(22));
System.out.println(normalJson.toString());
}
Map数据转 JSON
* Map数据转 JSON
*/
public static void JsonTest2() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "zhangsan"); // string 类型
map.put("age", new Integer(22)); // number 类型
Object obj = new String("params");
map.put("obj", obj); // Object 类型
map.put("array", new String[] { "a", "b", "c" }); // array 类型
map.put("b_true", Boolean.TRUE); // boolean 类型
map.put("b_false", Boolean.FALSE); // boolean 类型
JSONObject json = JSONObject.fromObject(map);
System.out.println(json.toString());
}
List 数据转 JSON
* List数据转 JSON
*/
public static void JsonTest3() {
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.print(jsonArray);
}
数组数据转 JSON
* 数组数据转 JSON
*/
public static void JsonTest4() {
String[] colors = new String[]{ "red", "yellow", "blue" };
JSONArray jsonArray = JSONArray.fromObject(colors);
System.out.println(jsonArray);
}
构建 JSON 文本
JSONStringer可以用来快速构建一个JSON格式的文本,并转换成String,可以写入文件;
JSONStringer是JSONWriter的子类;
JSONStringer一般通过object().key().value().key().value().endObject()进行构造;
object() 表明开始一个对象,即添加'{';
endObject() 表明结束一个对象,即添加'}';
array() 表明开始一个数组,即添加一个'[';
endArray() 表明结束一个数组,即添加一个']';
key() 表示添加一个key;
value() 表示添加一个value;
* 构建 JSON 文本
*/
public static void JsonTest5() {
JSONStringer stringer = new JSONStringer();
String str = stringer.object().key("product").value("phone").key("num").value(20).endObject().toString();
System.out.println(str);
}