JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
JSON 使用 JavaScript 语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。目前非常多的动态(PHP,JSP,.NET)编程语言都支持JSON。
对于 AJAX 应用程序来说,JSON 比 XML 更快更易使用:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据语法title>
<script>
//1.定义基本格式
var person = {"name":"张三",age:23,'gender':true}
alert(person)
//2.嵌套格式 {}->[]
var persons = {
"person":[
{"name":"张三","age":23,"gender":true},
{"name":"李四","age":24,"gender":true},
{"name":"王五","age":25,"gender":false}
]
};
alert(persons);
//嵌套格式 []->{}
var ps = [
{"name":"张三","age":23,"gender":true},
{"name":"李四","age":24,"gender":true},
{"name":"王五","age":25,"gender":false}
];
alert(ps);
script>
head>
<body>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据语法title>
<script>
//1.定义基本格式
var person = {"name":"张三",age:23,'gender':true}
alert(person)
//获取name的值
var name = person.name;
var name = person["name"];
alert(name);
//2.嵌套格式 {}->[]
var persons = {
"person":[
{"name":"张三","age":23,"gender":true},
{"name":"李四","age":24,"gender":true},
{"name":"王五","age":25,"gender":false}
]
};
alert(persons);
var name = persons.person[2].name;
alert(name);
//嵌套格式 []->{}
var ps = [
{"name":"张三","age":23,"gender":true},
{"name":"李四","age":24,"gender":true},
{"name":"王五","age":25,"gender":false}
];
//alert(ps);
var name = ps[1].name;
alert(name);
script>
head>
<body>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据语法title>
<script>
//1.定义基本格式
var person = {"name":"张三",age:23,'gender':true}
//嵌套格式 []->{}
var ps = [
{"name":"张三","age":23,"gender":true},
{"name":"李四","age":24,"gender":true},
{"name":"王五","age":25,"gender":false}
];
//获取person对象中所有的键和值
//for in 循环
for(var key in person){
//这样的方式获取不行,因为相当于person."name"
// alert(key + ":" + person.key);
alert(key + ":" + person[key]);
}
//获取ps中的所有值
for (var i = 0; i < ps.length; i++) {
var p=ps[i];
for(var key in p){
alert(key + ":" + p[key]);
}
}
script>
head>
<body>
body>
html>
ObjectMapper 转换方法:
writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换成JSON字符串,并将JSON数据填充到字符输出流中
OutputStream:将obj对象转换成JSON字符串,并将JSON数据填充到字节输出流中
writeValueAsString(obj):将对象转为json字符串
package cn.siyi.test;
import cn.siyi.domain.Person;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class JacksonTest {
//Java对象转为JSON字符串
@Test
public void test1() throws IOException {
//1.创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
//2.创建Jackson的核心对象 ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
//3.转换
// String json = objectMapper.writeValueAsString(p);
// System.out.println(json);
// objectMapper.writeValue(new File("D://a.txt"),p);
objectMapper.writeValue(new FileWriter("d://b.txt"),p);
}
@Test
public void test2() throws Exception {
//1.创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());
//2.转换
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(p);
System.out.println(json);
}
@Test
public void test03() throws Exception {
//1.创建Person对象
Person p = new Person();
p.setName("李四");
p.setAge(21);
p.setGender("男");
p.setBirthday(new Date());
Person p1 = new Person();
p1.setName("张三");
p1.setAge(22);
p1.setGender("男");
p1.setBirthday(new Date());
Person p2 = new Person();
p2.setName("张三");
p2.setAge(23);
p2.setGender("男");
p2.setBirthday(new Date());
//创建List集合
List<Person> ps = new ArrayList<Person>();
ps.add(p);
ps.add(p1);
ps.add(p2);
//转换
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(ps);
System.out.println(json);
}
@Test
public void test04() throws Exception {
//1.创建Map对象
Map<String, Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",23);
map.put("gender","男");
//转换
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(map);
System.out.println(json);
}
}
readValue(json字符串数据,Class)
package cn.siyi.test;
import cn.siyi.domain.Person;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class JacksonTest {
//json字符串转为Java对象
@Test
public void test05() throws Exception {
//1.初始化JSON字符串
String json = "{\"gender\":\"男\",\"name\":\"张三\",\"age\":23}";
//2.创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(json, Person.class);
System.out.println(person);
}
}