赞
踩
目录
JSON:JavaScript Object Notation JavaScript对象表示法;
在Java中我们可以封装信息如:
- Person p=new Person();
- p.setName("张三");
- p.setAge(19);
- p.setGender("男");
那么在JavaScript中怎样存储这样零散的数据呢?
var p={"name":"张三","age":23,"gender":"男"}
1)多用于存储和交换文本信息的语法;
2)进行数据的传输;
3)JSON相比于XML更小、更快、更易解析;
1)数据在名称/值对中:json数据是由键值对构成的,键用引号(单双都行)引起来,也可以不使用引号;
2)值得取值类型:
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true或false)
数组(在方括号中)
对象(在花括号中)
3)数据由逗号分隔:多个键值对由逗号分隔;
4)花括号保存对象:使用{}定义json格式;
5)方括号保存数组;
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JSON</title>
- <script>
- //1. 基本格式
- var person={"name":"张三","age":23,"gender":"男"};
-
- //2. 嵌套格式
- //2.1 {}嵌套[]
- var person2={
- "persons":[
- {"name":"小明","age":18,"gender":"男"},
- {"name":"张三","age":20,"gender":"男"},
- {"name":"小红","age":21,"gender":"女"}
- ]
- };
- //2.2 []嵌套{}
- var person3=[
- {"name":"小明","age":18,"gender":"男"},
- {"name":"张三","age":20,"gender":"男"},
- {"name":"小红","age":21,"gender":"女"}
- ];
-
- </script>
- </head>
- <body>
- </body>
- </html>
- 1. json对象.键名
- 2. json对象["键名"]
- 3. 数组对象[索引]
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JSON</title>
- <script>
- //1. 基本格式
- var person={"name":"张三","age":23,"gender":"男"};
-
- //2. 嵌套格式
- //2.1 {}嵌套[]
- var person2={
- "persons":[
- {"name":"小明","age":18,"gender":"男"},
- {"name":"张三","age":20,"gender":"男"},
- {"name":"小红","age":21,"gender":"女"}
- ]
- };
- //2.2 []嵌套{}
- var person3=[
- {"name":"小1","age":18,"gender":"男"},
- {"name":"张2","age":20,"gender":"男"},
- {"name":"小6","age":21,"gender":"女"}
- ];
- //3. 获取数据
- //3.1 获取张三
- alert(person.name);//也可通过alert(person[name]);获取
- //3.2 获取小红
- alert(person2.persons[2].name);
- //3.3 获取小6
- alert(person3[2].name);
- //3.4 获取person对象的所有键和值
- for(var key in person){
- //不能用person.key的方式获取,person.key相当于person."name";
- console.log(key+":"+person[key]);
- }
- //3.5 获取person3中的所有数据
- for(var i=0;i<person3.length;i++){
- var p=person3[i];
- for(var k in p){
- console.log(k+":"+p[k]);
- }
- }
- </script>
- </head>
- <body>
- </body>
- </html>
1)Jsonlib(官方)
2)Gson(谷歌)
3)fastjson(阿里巴巴)
4)jackson(SpringMVC内置解析器)
1)导入jackson的相关jar包;
jacksonjar包下载地址:https://mvnrepository.com/search?q=jackson
2)创建Jackson核心对象 ObjectMapper;
3)调用ObjectMapper的相关方法进行转换;
readValue(json字符串数据,Class)
-
- public class JacksonTest {
-
- @Test
- public void test() throws JsonProcessingException {
- //1. 初始化json字符串
- String json="{\"name\":\"小李\",\"age\":28,\"gender\":\"男\"}";
- //2. 创建ObjectMapper对象
- ObjectMapper objectMapper = new ObjectMapper();
- //3. 转换为Java对象 Person对象
- Person person = objectMapper.readValue(json, Person.class);
- System.out.println(person);
- }
- }
1)导入jackson的相关jar包;
jacksonjar包下载地址:https://mvnrepository.com/search?q=jackson
2)创建Jackson核心对象 ObjectMapper;
3)调用ObjectMapper的相关方法进行转换;
- public class JacksonTest {
- @Test
- public void test1() throws IOException {
- //1. 创建Person对象
- Person person = new Person();
- person.setName("小李");
- person.setAge(28);
- person.setGender("男");
- //2. 创建Jackson核心对象 ObjectMapper
- ObjectMapper objectMapper = new ObjectMapper();
- //3. 转换
- /*
- 转换方法
- writeValue(参数1,obj)
- 参数1:
- File:将obj对象转换为JSON字符串,并保存到指定的文件中
- Writer:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
- OutputStream
- writeValueAsString(obj):将对象转为json字符串
- */
- String s = objectMapper.writeValueAsString(person);
- System.out.println(s);
- objectMapper.writeValue(new File("D://random.txt"),person);
- }
- }
注:复杂java对象转换
List:数组
Map:与对象一致
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。