json的语法格式
json对象有三种数据格式,分别如下:
类型 | 语法 | 解释 |
---|---|---|
对象类型 | {name:value,name:value…} | 其中name是字符串类型,而value是任意类型 |
数组/集合类型 | [value,value,value…] | 其中value是任意类型 |
混合类型 | [{},{}… …] 或 {name:[]… …} | 合理包裹嵌套对象类型和数组类型 |
json格式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json01</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
</body>
<script language="JavaScript">
/**
* 案例一
* var person={key:value,key:value}
*
* class Person{
* String firstname = "张";
* String lastname = "三丰";
* Integer age = 100;
* }
*
* Person p = new Person();
* System.out.println(p.firstname);
*/
//json的定义
var person = {"firstname":"张","lastname":"三丰","age":100};
//json解析
alert(person.firstname);
alert(person.lastname);
alert(person.age);
</script>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json02</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
</body>
<script language="JavaScript">
/**
* 案例二
* [{key:value,key:value},{key:value,key:value}]
*
*/
var json = [
{"firstname":"张","lastname":"三丰","age":100},
{"firstname":"张","lastname":"翠山","age":58},
{"firstname":"张","lastname":"无忌","age":23}
];
for(var i=0;i<json.length;i++){
alert(json[i].lastname);
}
</script>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json03</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
</body>
<script language="JavaScript">
/**
* 案例三
* {
* "param":[{key:value,key:value},{key:value,key:value}]
* }
*
*
*/
var json = {
"baobao":[
{"name":"小双","age":18,"addr":"扬州"},
{"name":"建宁","age":18,"addr":"北京海淀"},
{"name":"龙儿","age":38,"addr":"岛国"},
{"name":"阿珂","age":17,"addr":"台湾"}
]
}
//全取
for(var i=0;i<json.baobao.length;i++){
alert(json.baobao[i].name);
}
</script>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>insertBefore.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
</body>
<script language="JavaScript">
/**
* 案例四
* {
* "param1":[{key:value,key:value},{key:value,key:value}],
* "param2":[{key:value,key:value},{key:value,key:value}],
* "param3":[{key:value,key:value},{key:value,key:value}]
* }
*
*
*/
var json = {
"baobao":[
{"name":"小双","age":18,"addr":"扬州"},
{"name":"建宁","age":18,"addr":"北京海淀"},
{"name":"龙儿","age":38,"addr":"岛国"},
{"name":"阿珂","age":17,"addr":"台湾"}
],
"haohao":[
{"name":"楠楠","age":23,"addr":"北京昌平修正"},
{"name":"倩倩","age":18,"addr":"上海"}
]
}
//娶楠楠
alert(json.haohao[0].name);
</script>
</html>
json的转换格式
json的转换工具是通过java封装好的一些jar工具包,直接将java对象或集合转换成json格式的字符串。
4.4.2 常见的json转换工具
工具名称 | 介绍 |
---|---|
Jsonlib | Java 类库,需要导入的jar包较多 |
Gson | google提供的一个简单的json转换工具 |
Fastjson | alibaba技术团队提供的一个高性能的json转换工具 |
Jackson | 开源免费的json转换工具,springmvc转换默认使用jackson |
使用方法
1)导入json相关jar包
2)创建java对象或集合
3) 使用jackson的ObjectMapper对象的writeValueAsString方法进行转换
package com.itheima.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDemo {
public static void main(String[] args) throws JsonProcessingException {
//创建User对象
User user = new User();
user.setId("100");
user.setUsername("haohao");
user.setAge(33);
//创建List集合
List<String> arr = new ArrayList<>();
arr.add("test1");
arr.add("test2");
arr.add("test3");
//创建Map集合
Map<String,User> map = new HashMap<>();
map.put("user", user);
//转换
ObjectMapper om = new ObjectMapper();
String userJson = om.writeValueAsString(user);
String arrJson = om.writeValueAsString(arr);
String mapJson = om.writeValueAsString(map);
System.out.println(userJson);
System.out.println(arrJson);
System.out.println(mapJson);
}
}