熱線(xiàn)電話(huà):0755-23712116
郵箱:contact@shuangyi-tech.com
地址:深圳市寶安區(qū)沙井街道后亭茅洲山工業(yè)園工業(yè)大廈全至科技創(chuàng)新園科創(chuàng)大廈2層2A
JSON全名稱(chēng)是JavaScript Object Notation,即JavaScript對(duì)象表示法。
Java的對(duì)象表示:
Person p = new Person();
p.setName("張三");
p.setAge(23);
p.setGender("男");
JavaScript的對(duì)象表示:
var p = {"name":"張三","age":23,"gender":"男"};
JSON介紹:
JSON語(yǔ)法基本規(guī)則
1、數(shù)據(jù)在鍵/值對(duì)中:JSON數(shù)據(jù)由鍵值對(duì)構(gòu)成。
# 值是數(shù)字類(lèi)型
{age: 10, score: 78.5}
# 值是字符串類(lèi)型
{name: "張三", sex: "m"}
# 值是布爾值類(lèi)型
{flag: true}
# 值是數(shù)組類(lèi)型
{names: ["zhangsan", "lisi", "wangwu"]}
{students: [{name: "zhangsan"}, {name: "lisi"}]}
# 值是對(duì)象類(lèi)型
{student: {name: "zhangsan", age: 18, flag: true}}
# 值是null類(lèi)型
{value: null}
2、數(shù)據(jù)由逗號(hào)分隔:多個(gè)鍵值對(duì)用逗號(hào)進(jìn)行分隔
{key1: value1, key2: value2, key3: value3, ...}
3、花括號(hào)保存對(duì)象:使用{}定義json格式的對(duì)象,又可以嵌套其他數(shù)據(jù)類(lèi)型的值。
{student: {name: "zhangsan", hobbies: ["play", "read", "listen"]}}
4、方括號(hào)保存數(shù)組:[1,2,3,4,5],又可以嵌套其他數(shù)據(jù)類(lèi)型的值。
[{name1:"zhangsan"},{name2:"lisi"}]
[1,2,3,4,5,6]
讀取JSON數(shù)據(jù)
讀取通常是指在前端使用JavaScript讀取JSON格式數(shù)據(jù),后端Java不采用此語(yǔ)法:
# 讀取對(duì)象類(lèi)型中指定鍵的值
json對(duì)象.鍵名
json對(duì)象["鍵名"]
# 讀取JSON數(shù)組中的值
數(shù)組對(duì)象[索引]
# 遍歷對(duì)象
for (鍵名 in 對(duì)象) {
# key為鍵名;對(duì)象[key]為鍵值
}
# 遍歷數(shù)組
for (var i=0; i<數(shù)組.length; i++) {
# 數(shù)組[i]獲取數(shù)組的值
}
實(shí)例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json遍歷</title>
</head>
<body>
<script>
// 遍歷對(duì)象
// 鍵名可以不加引號(hào),也可以加單引號(hào)或雙引號(hào)
var person1 = {name: "張三", age: 23, sex: "m"};
var person2 = {"name": "張三", "age": 23, "sex": "m"};
var person3 = {'name': "張三", 'age': 23, 'sex': "m"};
// 獲取person對(duì)象中所有的鍵和值
// for in 循環(huán)
for (key in person1) {
// 無(wú)法使用person1.key,因?yàn)檫@樣相當(dāng)于person1."name"
console.log(key, person1[key]);
}
// 遍歷普通數(shù)組
var arr=["唐僧","孫悟空","豬八戒","沙僧"];
for (var i=0; i < arr.length; i++)
{
console.log(arr[i]);
}
// 遍歷對(duì)象數(shù)組
var ps = [{"name": "張三", "age": 23, "gender": true},
{"name": "李四", "age": 24, "gender": true},
{"name": "王五", "age": 25, "gender": false}];
for (var i = 0; i < ps.length; i++) {
var p = ps[i];
for (key in p) {
console.log(key + ":" + p[key]);
}
}
</script>
</body>
</html>
概述
要想實(shí)現(xiàn)JSON數(shù)據(jù)與Java數(shù)據(jù)的相互轉(zhuǎn)換,需要JSON解析器,即能夠解析JSON格式數(shù)據(jù)的jar包。
常見(jiàn)的解析器:Jsonlib,Gson,fastjson,jackson。
本節(jié)中使用jackson,因?yàn)閟pring內(nèi)置它,為以后打下基礎(chǔ)。
JSON轉(zhuǎn)為Java對(duì)象
將JSON轉(zhuǎn)換為Java對(duì)象的步驟如下:
1. 導(dǎo)入jackson的相關(guān)jar包
2. 創(chuàng)建Jackson核心對(duì)象 ObjectMapper
3. 調(diào)用ObjectMapper的相關(guān)方法進(jìn)行轉(zhuǎn)換:readValue(json字符串?dāng)?shù)據(jù), Class)
第一步:導(dǎo)入相關(guān)jar包(注意,版本一定要匹配,三個(gè)版本應(yīng)該一致,否則可能發(fā)生異常)
第二步:轉(zhuǎn)換對(duì)象
public class Json2Java {
public static void main(String[] args) throws IOException {
ObjectMapper mapper=new ObjectMapper();
String json="{\"username\":\"李四\",\"password\":\"123456\"}";
User user = mapper.readValue(json, User.class);
System.out.println(user);
}
}
class User{
private String username;
private String password;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}