JSON
-
JSON: JavaScript Object Notation(JavaScript 对象表示法)
-
JSON 是存储和交换文本信息的语法,类似 XML。
-
JSON 比 XML 更小、更快,更易解析。
JSON 语法规则
- 数据在名称/值对中
- 数据由逗号分隔
- 大括号 {} 保存对象
- 中括号 [] 保存数组,数组可以包含多个对象
JSON vs XML
- JSON 和 XML 都用于接收 web 服务端的数据。
- JSON 和 XML在写法上有所不同,如下所示:
xml
<?xml version="1.0" encoding="UTF-8"?>
<person>
<person id="2022" gender="1">张三</person>
<person id="2023" gender="0">李四</person>
</person>
json
{
"person":[
{
"nickname":"张三",
"gender":"1"
},
{
"nickname":"李四",
"gender":"2"
}
]
}
JSON 对象语法
- JSON 对象使用在大括号({})中书写。
- 对象可以包含多个 key/value(键/值)对。
- key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。
- key 和 value 中使用冒号(:)分割。
- 每个 key/value 对使用逗号(,)分割。
json Gson fastJson基本使用
Emp类
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
/**
* 账号
*/
private String account;
/**
* 密码
*/
private String password;
/**
* 昵称
*/
private String nickname;
/**
* 性别 1代表男性 2代表女性
*/
private Integer gender;
/**
* 出生日期
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date birthday;
@Override
public String toString() {
return "Emp{" +
"account='" + account + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
", gender=" + gender +
", birthday=" + BashDate.getDateString(birthday) +
'}';
}
}
BashDate抽象类
public abstract class BashDate {
/**
* 默认时间字符串模板
*/
private final static String pattern = "yyyy-MM-dd HH:mm:ss";
/**
* 日期格式化
* @param date 日期
* @return
*/
public static String getDateString(Date date){
return getDateString(date,pattern);
}
/**
* 日期格式化
* @param date 日期
* @param pattern 格式样式
* @return 日期格式化后的字符串
*/
public static String getDateString(Date date,String pattern){return new SimpleDateFormat(pattern).format(date);}
/**
* 日期格式化
* @param source 被解析的日期字符串
* @return 解析后的日期
*/
public static Date parse(String source) throws ParseException {
return new SimpleDateFormat(pattern).parse(source);
}
}
Gson
对象转字符串
public class GsonJsonTest {
// Gson gson = new Gson();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
@Test
public void bean2string(){
try {
Emp emp = new Emp("admin","123456","张三",1, BashDate.parse("1990-11-11 15:30:00");
String jsonString = gson.toJson(emp);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
}
字符串转对象
public class GsonJsonTest {
@Test
public void String2bean(){
String text = "{\"account\":\"admin\",\"birthday\":\"1990-11-11 15:30:00\",\"gender\":1,\"nickname\":\"张三\",\"password\":\"123456\"}";
Emp emp = gson.fromJson(text, Emp.class);
System.out.println(emp.toString());
}
}
集合转字符串
public class GsonJsonTest {
@Test
public void list2string(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
String jsonString = gson.toJson(list);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
}
字符串转集合
public class GsonJsonTest {
@Test
public void string2list(){
try {
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
String jsonString = gson.toJson(list);
System.out.println(jsonString);
List<Emp> empList = gson.fromJson(jsonString, new TypeToken<List<Emp>>(){}.getType());
empList.forEach(emp -> {
System.out.println(emp);
});
}catch (ParseException e){
e.printStackTrace();
}
}
}
map转字符串
public class GsonJsonTest {
@Test
public void map2string(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
Map<String, List<Emp>> map = new HashMap<>();
map.put("emps",list);
String jsonString = gson.toJson(map);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
}
字符串转map
public class GsonJsonTest {
@Test
public void string2map(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
Map<String, List<Emp>> map = new HashMap<>();
map.put("emps",list);
String jsonString = gson.toJson(map);
System.out.println(jsonString);
Map<String,List<Emp>> stringListHasMap = gson.fromJson(jsonString,new TypeToken<Map<String,List<Emp>>>(){}.getType());
if (stringListHasMap.containsKey("emps")){
List<Emp> emps = stringListHasMap.get("emps");
emps.forEach(emp -> {
System.out.println(emp.toString());
});
}
}catch (ParseException e){
e.printStackTrace();
}
}
}
FastJSON
对象转字符串
public class FastJsonTest {
@Test
public void bean2string(){
try {
Emp emp = new Emp("admin","123456","张三",1, BashDate.parse("1990-11-11 15:30:00");
String jsonString = JSON.toJSONString(emp);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
}
字符串转对象
public class FastJsonTest {
@Test
public void String2bean(){
String text = "{\"account\":\"admin\",\"birthday\":\"1990-11-11 15:30:00\",\"gender\":1,\"nickname\":\"张三\",\"password\":\"123456\"}";
Emp emp = JSON.parseObject(text, Emp.class);
System.out.println(emp.toString());
}
}
集合转字符串
public class FastJsonTest {
@Test
public void list2string(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
}
字符串转集合
public class FastJsonTest {
@Test
public void string2list(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
List<Emp> empList = JSON.parseArray(jsonString, Emp.class);
empList.forEach(emp -> {
System.out.println(emp);
});
}catch (ParseException e){
e.printStackTrace();
}
}
}
map转字符串
public class FastJsonTest {
@Test
public void map2string(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
Map<String, List<Emp>> map = new HashMap<>();
map.put("emps",list);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
}
字符串转map
@Test
public void string2map(){
try {
List<Emp> list = new ArrayList<>();
list.add(new Emp("admin", "123456", "张三", 1, BashDate.parse("1990-11-11 15:30:00")));
list.add(new Emp("admin", "123456", "李四", 1, BashDate.parse("1990-11-11 20:30:00")));
Map<String, List<Emp>> map = new HashMap<>();
map.put("emps",list);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
Map<String,List<Emp>> stringListHasMap = JSON.parseObject(jsonString,new TypeReference<Map<String,List<Emp>>>(){
});
if (stringListHasMap.containsKey("emps")){
List<Emp> emps = stringListHasMap.get("emps");
emps.forEach(emp -> {
System.out.println(emp.toString());
});
}
}catch (ParseException e){
e.printStackTrace();
}
}
}