当前位置:   article > 正文

【Java干货教程】JSON,JSONObject,JSONArray类详解_java中 jsonarray类型

java中 jsonarray类型

一、定义

JSON:就是一种轻量级的数据交换格式,被广泛应用于WEB应用程序开发。JSON的简洁和清晰的层次结构,易于阅读和编写;同时也易于机器解析和生成,有效的提升网络传输效率;支持多种语言,很多流行的语言都对JSON格式有着很友好的支持。

  • JSON对象:就是多个属性被{}括起来的。
  • JSON数组:就是包含了多个JSON对象的一个集合,数组是以数组括号[]括起来的。JSON数组并不一定是要相同的JSON对象的集合,也可以是不同的对象。

JSON、JSON对象、JSON数组的区别 

  • JSON是一种数据结构,类型xml;
  • JSON对象则是对JSON的具体体现;JSON数组则是将多个JSON对象进行存储的一个集合。

这里以fastjson2来进行讲解,不同的jar包对JSON相关的处理有着不同的实现方式,但是大部分方法也都是相同的。

二、fastjson2

2.1、fastjson2简介

fastjson2 是 FASTJSON 项目的重要升级,目标是为下一个十年提供一个高性能的JSON库, fastjson2 性能相比原先旧的 fastjson 有了很大提升,并且 fastjson2 更安全,完全删除autoType白名单,提升了安全性。

中文文档: 

​​​​https://github.com/alibaba/fastjson2/blob/main/README.md

下面是一些常用的方法 

2.2、导入fastjson2依赖

maven如下:pom(本文所有代码仅使用这一个依赖即可):

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba.fastjson2</groupId>
  4. <artifactId>fastjson2</artifactId>
  5. <version>2.0.26</version>
  6. </dependency>
  7. </dependencies>

需要注意的一点是在使用 fastjson2 时导入的包名是 com.alibaba.fastjson2 ,就像下面这样:

import com.alibaba.fastjson2.JSON;

import com.alibaba.fastjson2.JSONObject;

import com.alibaba.fastjson2.JSONArray;

2.3、json对象与json数组的创建

2.3.1、json对象创建

  1. import com.alibaba.fastjson2.JSONObject;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. JSONObject info = new JSONObject();
  5. info.put("name", "张三");
  6. info.put("age", "18");
  7. info.put("地理", "70");
  8. info.put("英语", "60");
  9. System.out.println(info);
  10. }
  11. }

2.3.2、json数组创建

  1. import com.alibaba.fastjson2.JSONArray;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. JSONArray array = new JSONArray();
  5. array.add("1班");
  6. array.add("2班");
  7. array.add("3班");
  8. System.out.println(array);
  9. }
  10. }

2.3.2、json对象添加到json数组

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject info1 = new JSONObject();
  6. info1.put("name", "张三");
  7. info1.put("age", "18");
  8. JSONObject info2 = new JSONObject();
  9. info2.put("name", "李四");
  10. info2.put("age", "19");
  11. //把上面创建的两个json对象加入到json数组里
  12. JSONArray array = new JSONArray();
  13. array.add(info1);
  14. array.add(info2);
  15. System.out.println(array);
  16. }
  17. }

2.4、json对象取值与json数组遍历取值

2.4.1、json对象取值

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONArray array = new JSONArray();
  6. array.add("1班");
  7. array.add("2班");
  8. array.add("3班");
  9. JSONObject school = new JSONObject();
  10. school.put("schoolName", "第一中学");
  11. school.put("teacher", "刘梅");
  12. JSONObject info = new JSONObject();
  13. info.put("name", "张三");
  14. info.put("age", "18");
  15. info.put("gradle", array);
  16. info.put("schoolInfo", school);
  17. //从info中取值
  18. System.out.println(info.getString("name")); //张三
  19. System.out.println(info.getIntValue("age"));//18
  20. System.out.println(info.getJSONArray("gradle"));//["1班","2班","3班"]
  21. System.out.println(info.getJSONObject("schoolInfo"));//{"schoolName":"第一中学","teacher":"刘梅"}
  22. }
  23. }

2.4.2、json数组遍历取值 

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject info1 = new JSONObject();
  6. info1.put("name", "张三");
  7. info1.put("age", "18");
  8. JSONObject info2 = new JSONObject();
  9. info2.put("name", "李四");
  10. info2.put("age", "19");
  11. JSONArray array = new JSONArray();
  12. array.add(info1);
  13. array.add(info2);
  14. //遍历获取json数组中对象的值
  15. for (int i = 0; i < array.size(); i++) {
  16. JSONObject json = array.getJSONObject(i);
  17. System.out.println(json.getString("name"));
  18. System.out.println(json.getString("age"));
  19. }
  20. }
  21. }

2.5、json对象与字符串的转换 

2.5.1、json对象与字符串的转换

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject info = new JSONObject();
  6. info.put("name", "张三");
  7. info.put("age", "18");
  8. info.put("地理", "70");
  9. info.put("英语", "60");
  10. //JSON对象转字符串
  11. String str = JSON.toJSONString(info);
  12. System.out.println(str);
  13. //JSON字符串转JSON对象
  14. JSONObject json = JSONObject.parseObject(str);
  15. System.out.println(json);
  16. }
  17. }

2.5.2、json字符串的字节数组转json对象

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. String str = "{\"name\":\"张三\",\"age\":\"18\",\"地理\":\"70\",\"英语\":\"60\"}";
  6. byte[] bytes = str.getBytes();
  7. JSONObject data = JSON.parseObject(bytes);
  8. System.out.println(data);
  9. }
  10. }

2.6、json数组与字符串的转换

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONArray;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. String text = "[\"张三\",\"李四\",\"王五\"]";
  6. System.out.println(text);
  7. //json字符串转json数组
  8. JSONArray data = JSON.parseArray(text);
  9. //json数组转json字符串
  10. String str = JSONArray.toJSONString(data);
  11. System.out.println(data);
  12. System.out.println(str);
  13. }
  14. }

2.7、json字符串转java对象的转换 

Student类如下:

  1. public class Student {
  2. public String name;
  3. public int age;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. public Student(String name, int age) {
  17. this.name = name;
  18. this.age = age;
  19. }
  20. }

2.7.1、json字符串转java对象的转换

  1. import com.alibaba.fastjson2.JSON;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Student student = new Student("张三", 18);
  5. //Student对象转JSON字符串
  6. String studentStr = JSON.toJSONString(student);
  7. //JSON字符串转Student对象
  8. Student data = JSON.parseObject(studentStr, Student.class);
  9. System.out.println(studentStr);
  10. }
  11. }

2.7.2、java对象转byte数组转换 

  1. import com.alibaba.fastjson2.JSON;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Student student = new Student("张三", 18);
  5. //Student对象转字节数组
  6. byte[] text = JSON.toJSONBytes(student);
  7. //字节数组转Student对象
  8. Student data = JSON.parseObject(text, Student.class);
  9. System.out.println(data.getName() + data.getAge());
  10. }
  11. }

2.8、json字符串与Map转换 

2.8.1、json字符串转Map

  1. @Test
  2. public void test05(){
  3. String str = "{\n" +
  4. "\"gradle\":\"高一\",\n" +
  5. "\"number\":\"2\",\n" +
  6. "\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
  7. " {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
  8. "}";
  9. Map<String, Object> map1 = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>() {
  10. });
  11. Map map2 = JSON.parseObject(str, Map.class);
  12. System.out.println(map1);
  13. System.out.println(map2);
  14. }

2.8.2、Map转json字符串

(注意:如果直接使用JSON.toJSONString(map)转换,因为"测试1"的值为null,转换的结果就会是 {“测试2”:“hello”})

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONWriter;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class Demo {
  6. public static void main(String[] args) {
  7. Map<String, String> map = new HashMap<>();
  8. map.put("测试1", null);
  9. map.put("测试2", "hello");
  10. //{"测试2":"hello","测试1":null}
  11. String str = JSON.toJSONString(map, JSONWriter.Feature.WriteMapNullValue);
  12. System.out.println(str);
  13. }
  14. }

如果你使用的是老的fastjson1(下述是简单示例,不可使用),可以像下面这样转换:

  1. Map<String, String> map = new HashMap<>();
  2. map.put("测试1", null);
  3. map.put("测试2", "hello");
  4. String str = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue) ;

2.9、json数组转List

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class Demo {
  6. public static void main(String[] args) {
  7. String str = "{\n" +
  8. "\"gradle\":\"高一\",\n" +
  9. "\"number\":\"2\",\n" +
  10. "\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
  11. " {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
  12. "}";
  13. JSONObject strJson = JSONObject.parseObject(str);
  14. //获取people数组
  15. JSONArray people = strJson.getJSONArray("people");
  16. //json数组转List
  17. List<Map> list = people.toJavaList(Map.class);
  18. System.out.println(str);
  19. }
  20. }

如果你使用的是老的fastjson1,可以像下面这样转换:    

  1. String str = "{\n" +
  2. "\"gradle\":\"高一\",\n" +
  3. "\"number\":\"2\",\n" +
  4. "\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
  5. " {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
  6. "}";
  7. JSONObject strJson = JSONObject.parseObject(str);//字符串转json对象
  8. String people = String.valueOf(strJson.getJSONArray("people"));
  9. List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(people);

 

参考文章:JSONObject详解(com.alibaba)-fastjson2_com.alibaba.fastjson2-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/435064
推荐阅读
相关标签
  

闽ICP备14008679号