• java:解析json的几种方式


    Java是一种流行的编程语言,它提供了很多实用的库和工具,在处理JSON数据时也不例外。在本文中,我们将介绍Java中如何解析JSON数据。

    JSON是一种轻量级的数据交换格式,它已经成为Web应用程序中最流行的数据格式之一。Java提供了许多库来处理JSON数据,包括Jackson、Gson和JSON.simple等。

    1. 使用Jackson解析JSON

    Jackson是一个流行的Java库,它可以轻松处理JSON数据。首先,我们需要将其添加到项目的依赖中。

    
        com.fasterxml.jackson.core
        jackson-databind
        2.11.3
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    假设我们有以下JSON数据:

    {
        "name": "John",
        "age": 30,
        "city": "New York"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    我们可以使用以下代码将其解析为Java对象:

    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
            ObjectMapper mapper = new ObjectMapper();
            Person person = mapper.readValue(jsonString, Person.class);
            System.out.println(person.getName()); // Output: John
        }
    
        private static class Person {
            private String name;
            private int age;
            private String city;
    
            public String getName() {
                return name;
            }
    
            public int getAge() {
                return age;
            }
    
            public String getCity() {
                return city;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public void setAge(int age) {
                this.age = age;
            }
    
            public void setCity(String city) {
                this.city = city;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    1. 使用Gson解析JSON

    Gson是另一个流行的Java库,它可以解析JSON数据。为了使用Gson,我们需要将其添加到项目的依赖中。

    
        com.google.code.gson
        gson
        2.8.6
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    对于上面的JSON数据,我们可以使用以下代码将其解析为Java对象:

    import com.google.gson.Gson;
    
    public class Main {
        public static void main(String[] args) {
            String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
            Gson gson = new Gson();
            Person person = gson.fromJson(jsonString, Person.class);
            System.out.println(person.getName()); // Output: John
        }
    
        private static class Person {
            private String name;
            private int age;
            private String city;
    
            public String getName() {
                return name;
            }
    
            public int getAge() {
                return age;
            }
    
            public String getCity() {
                return city;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public void setAge(int age) {
                this.age = age;
            }
    
            public void setCity(String city) {
                this.city = city;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    1. 使用JSON.simple解析JSON

    JSON.simple是另一个流行的Java库,它提供了一些简单的API来解析JSON数据。同样,我们需要将其添加到项目的依赖中。

    
        com.googlecode.json-simple
        json-simple
        1.1.1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    对于上面的JSON数据,我们可以使用以下代码将其解析为Java对象:

    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonString);
            String name = (String) json.get("name");
            System.out.println(name); // Output: John
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    总的来说,Java提供了多种解析JSON数据的选项,其中包括Jackson、Gson和JSON.simple等流行的库。您可以选择其中一个库来解析JSON数据,并选择适合您项目的选项。

  • 相关阅读:
    IMX6ULL —— 字符编码方式和实现
    mybatisplus代码生成覆盖
    session会话机制
    如何重置iPhone的网络设置?这里提供详细步骤
    Uber:Java中的不稳定单元测试处理
    机器学习基础之《回归与聚类算法(3)—线性回归优化:岭回归》
    关于iOS:如何使用SwiftUI调整图片大小?
    【C++学习笔记】1.3 缺省参数
    MySQL-视图
    项目实战——配置MySQL与Spring Security模块
  • 原文地址:https://blog.csdn.net/weixin_43972437/article/details/133710997