JSON:就是一种轻量级的数据交换格式,被广泛应用于WEB应用程序开发。JSON的简洁和清晰的层次结构,易于阅读和编写;同时也易于机器解析和生成,有效的提升网络传输效率;支持多种语言,很多流行的语言都对JSON格式有着很友好的支持。
JSON、JSON对象、JSON数组的区别
这里以fastjson2来进行讲解,不同的jar包对JSON相关的处理有着不同的实现方式,但是大部分方法也都是相同的。
fastjson2 是 FASTJSON 项目的重要升级,目标是为下一个十年提供一个高性能的JSON库, fastjson2 性能相比原先旧的 fastjson 有了很大提升,并且 fastjson2 更安全,完全删除autoType白名单,提升了安全性。
中文文档:
https://github.com/alibaba/fastjson2/blob/main/README.md
下面是一些常用的方法
maven如下:pom(本文所有代码仅使用这一个依赖即可):
com.alibaba.fastjson2
fastjson2
2.0.26
需要注意的一点是在使用 fastjson2 时导入的包名是 com.alibaba.fastjson2 ,就像下面这样:
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
public class Demo {
public static void main(String[] args) {
JSONObject info = new JSONObject();
info.put("name", "张三");
info.put("age", "18");
info.put("地理", "70");
info.put("英语", "60");
System.out.println(info);
}
}
import com.alibaba.fastjson2.JSONArray;
public class Demo {
public static void main(String[] args) {
JSONArray array = new JSONArray();
array.add("1班");
array.add("2班");
array.add("3班");
System.out.println(array);
}
}
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
public class Demo {
public static void main(String[] args) {
JSONObject info1 = new JSONObject();
info1.put("name", "张三");
info1.put("age", "18");
JSONObject info2 = new JSONObject();
info2.put("name", "李四");
info2.put("age", "19");
//把上面创建的两个json对象加入到json数组里
JSONArray array = new JSONArray();
array.add(info1);
array.add(info2);
System.out.println(array);
}
}
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
public class Demo {
public static void main(String[] args) {
JSONArray array = new JSONArray();
array.add("1班");
array.add("2班");
array.add("3班");
JSONObject school = new JSONObject();
school.put("schoolName", "第一中学");
school.put("teacher", "刘梅");
JSONObject info = new JSONObject();
info.put("name", "张三");
info.put("age", "18");
info.put("gradle", array);
info.put("schoolInfo", school);
//从info中取值
System.out.println(info.getString("name")); //张三
System.out.println(info.getIntValue("age"));//18
System.out.println(info.getJSONArray("gradle"));//["1班","2班","3班"]
System.out.println(info.getJSONObject("schoolInfo"));//{"schoolName":"第一中学","teacher":"刘梅"}
}
}
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
public class Demo {
public static void main(String[] args) {
JSONObject info1 = new JSONObject();
info1.put("name", "张三");
info1.put("age", "18");
JSONObject info2 = new JSONObject();
info2.put("name", "李四");
info2.put("age", "19");
JSONArray array = new JSONArray();
array.add(info1);
array.add(info2);
//遍历获取json数组中对象的值
for (int i = 0; i < array.size(); i++) {
JSONObject json = array.getJSONObject(i);
System.out.println(json.getString("name"));
System.out.println(json.getString("age"));
}
}
}
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
public class Demo {
public static void main(String[] args) {
JSONObject info = new JSONObject();
info.put("name", "张三");
info.put("age", "18");
info.put("地理", "70");
info.put("英语", "60");
//JSON对象转字符串
String str = JSON.toJSONString(info);
System.out.println(str);
//JSON字符串转JSON对象
JSONObject json = JSONObject.parseObject(str);
System.out.println(json);
}
}
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
public class Demo {
public static void main(String[] args) {
String str = "{\"name\":\"张三\",\"age\":\"18\",\"地理\":\"70\",\"英语\":\"60\"}";
byte[] bytes = str.getBytes();
JSONObject data = JSON.parseObject(bytes);
System.out.println(data);
}
}
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
public class Demo {
public static void main(String[] args) {
String text = "[\"张三\",\"李四\",\"王五\"]";
System.out.println(text);
//json字符串转json数组
JSONArray data = JSON.parseArray(text);
//json数组转json字符串
String str = JSONArray.toJSONString(data);
System.out.println(data);
System.out.println(str);
}
}
Student类如下:
public class Student {
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
import com.alibaba.fastjson2.JSON;
public class Demo {
public static void main(String[] args) {
Student student = new Student("张三", 18);
//Student对象转JSON字符串
String studentStr = JSON.toJSONString(student);
//JSON字符串转Student对象
Student data = JSON.parseObject(studentStr, Student.class);
System.out.println(studentStr);
}
}
import com.alibaba.fastjson2.JSON;
public class Demo {
public static void main(String[] args) {
Student student = new Student("张三", 18);
//Student对象转字节数组
byte[] text = JSON.toJSONBytes(student);
//字节数组转Student对象
Student data = JSON.parseObject(text, Student.class);
System.out.println(data.getName() + data.getAge());
}
}
@Test
public void test05(){
String str = "{\n" +
"\"gradle\":\"高一\",\n" +
"\"number\":\"2\",\n" +
"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
" {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
"}";
Map map1 = JSONObject.parseObject(str, new TypeReference
(注意:如果直接使用JSON.toJSONString(map)转换,因为"测试1"的值为null,转换的结果就会是 {“测试2”:“hello”})
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import java.util.HashMap;
import java.util.Map;
public class Demo {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("测试1", null);
map.put("测试2", "hello");
//{"测试2":"hello","测试1":null}
String str = JSON.toJSONString(map, JSONWriter.Feature.WriteMapNullValue);
System.out.println(str);
}
}
如果你使用的是老的fastjson1(下述是简单示例,不可使用),可以像下面这样转换:
Map map = new HashMap<>();
map.put("测试1", null);
map.put("测试2", "hello");
String str = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue) ;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.util.List;
import java.util.Map;
public class Demo {
public static void main(String[] args) {
String str = "{\n" +
"\"gradle\":\"高一\",\n" +
"\"number\":\"2\",\n" +
"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
" {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
"}";
JSONObject strJson = JSONObject.parseObject(str);
//获取people数组
JSONArray people = strJson.getJSONArray("people");
//json数组转List
List
如果你使用的是老的fastjson1,可以像下面这样转换:
String str = "{\n" +
"\"gradle\":\"高一\",\n" +
"\"number\":\"2\",\n" +
"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
" {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
"}";
JSONObject strJson = JSONObject.parseObject(str);//字符串转json对象
String people = String.valueOf(strJson.getJSONArray("people"));
List
参考文章:JSONObject详解(com.alibaba)-fastjson2_com.alibaba.fastjson2-CSDN博客