Java Web 从入门到退坑 —— 第十四章 JSON
By: -gregPerlinLi-
1. 什么是 JSON
JSON(JavaScript Object Notation)是一种轻量级的交换格式,易于人阅读和编写,同时也易于机器解析和生成。JSON 采用完全独立于语言的文本格式,而且很多语言都提供了对 JSON 的支持(包括C,C++,C#,Java,JavaScript,Perl,Python等)。这样使得 JSON 成为理想的数据交换语言。
JSON 是一种轻量级的数据交换格式。
1. 轻量级: 指的是和 XML 做比较。
2. 数据交换: 指的是客户端和服务器之间业务数据的传输格式。
2. JSON 在 JavaScript 中的使用
2.1. JSON 的定义
JSON 是由键值对组成,并且由花括号(大括号){}
包围,每个键由引号 “”
引起来,键和值之间使用冒号 :
进行分割,多组键值对之间使用逗号 ,
进行分割。
示例代码:
var jsonObj = {
"key1" : 12,
"key2" : "abc",
"key3" : true,
"key4" : [11, "arr", false],
"key5" : {
"key5_1" : 551,
"key5_2" : "key5-2-value",
"key5_3" : true,
},
"key6" : [{
"key6_1_1" : 6611,
"key6_1_2" : "key6-1-2-value",
"key6_1_3" : false
}, {
"key6_2_1" : 6621,
"key6_2_2" : "key6-2-2-value",
"key6_2_3" : true
}]
};
2.2. JSON 的访问
JSON 本身就是一个对象。
JSON 中的 key
可以理解为对象中的一个属性。
JSON 中的 key
访问就和访问对象的属性一样,jsonObj.key
。
JSON 访问示例:
alert(jsonObj.key1); // 12
alert(jsonObj.key2); // abc
alert(jsonObj.key3); // true
alert(jsonObj.key4); // 11,arr,false
for ( var i = 0; i < jsonObj.key4.length; i++ ) {
alert(jsonObj.key4[i]);
}
alert(jsonObj.key5.key5_1); // 551
alert(jsonObj.key5.key5_2); // key5-2-value
alert(jsonObj.key6); // [object Object],[object Object]
// Every element taken out is a JSON object
var jsonItems = jsonObj.key6[0];
alert(jsonItems.key6_1_1); // 6611
alert(jsonItems.key6_1_2); // key6-1-2-value
2.3. JSON 的两个常用方法
JSON 的存在有两种形式:
1. 对象的形式存在,即为 JSON
对象。
2. 字符串的形式存在,即为 JSON
字符串。
一般要操作 JSON 中的数据的时候,需要 JSON
对象的格式。
一般我们要在客户端和服务器之间进行数据交换的时候,使用 JSON
字符串。
JSON.stringify()
把 JSON
对转换成为 JSON
字符串
JSON.parse()
把 JSON
字符串转换成为 JSON 对象
示例代码:
// JSON object string
var jsonObjString = JSON.stringify(jsonObj); // Like toString() method in Java
alert(jsonObjString);
// JSON string to JSON object
var jsonObj2 = JSON.parse(jsonObjString);
alert(jsonObj2.key1); // 12
alert(jsonObj2.key2); // abc
3. JSON 在 Java 中的使用
JSON 在 Java 中使用需要用到额外的 jar
包,这里是用的是 Google 的 Gson.jar
3.1. JavaBean 和 JSON 的互转
示例代码:
/**
* 1. Mutual transformation between JavaBean and JSON
*/
@Test
public void test1() {
Person person = new Person(1, "XiaoMing");
// Create a Gson object instance
Gson gson = new Gson();
// toJson() method can convert Java objects into JSON strings
String personJsonString = gson.toJson(person);
System.out.println(personJsonString);
// formJson() method converting JSON characters back to Java objects
// The first parameter is the JSON string
// The second parameter is the type of Java object converted back
Person person1 = gson.fromJson(personJsonString, Person.class);
System.out.println(person1);
}
3.2. List 和 JSON 的互传
示例代码:
/**
* 2. Mutual transmission of List and JSON
*/
public void test2() {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person(1, "XiaoMing"));
personList.add(new Person(2, "XiaoHong"));
personList.add(new Person(3, "XiaoJun"));
personList.add(new Person(4, "XiaoQiang"));
personList.add(new Person(5, "XiaoQing"));
Gson gson = new Gson();
// Convert the list collection to a JSON string
String personListJsonString = gson.toJson(personList);
System.out.println(personListJsonString);
// formJson() method converting JSON characters back to List collection
// You need to create a new anonymous inner class clas to convert the JSON to List<Person>
List<Person> personList1 = gson.fromJson(personListJsonString, new TypeToken<ArrayList<Person>>(){}.getType());
System.out.println(personList1);
}
3.3. Map 和 JSON 的互传
示例代码:
/**
* 3. Mutual transmission of Map and JSON
*/
@Test
public void test3() {
Map<Integer, Person> personMap = new HashMap<>();
personMap.put(1, new Person(1, "XiaoMing"));
personMap.put(2, new Person(2, "XiaoHong"));
personMap.put(3, new Person(3, "XiaoJun"));
personMap.put(4, new Person(4, "XiaoQiang"));
personMap.put(5, new Person(5, "XiaoQing"));
Gson gson = new Gson();
// Convert map set to JSON string
String personMapJsonString = gson.toJson(personMap);
System.out.println(personMapJsonString);
// formJson() method converting JSON characters back to Map set
// You need to create a new anonymous inner class class to convert the JSON to Map<Integer, Person>
Map<Integer, Person> personMap1 = gson.fromJson(personMapJsonString, new TypeToken<HashMap<Integer, Person>>(){}.getType());
System.out.println(personMap1);
Person person = personMap1.get(1);
System.out.println(person);
}