Json处理总结

163 阅读3分钟

常用的就是 Jackson和Fastjson

假设我们有一个简单的 Java 类 User

package com.itheima.pojo;

/**
 * @author wuJiaWei
 * @version 1.0
 */
public class User {
    private Integer id;
    private String name;
    private Short age;
    private Short gender;
    private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public Short getGender() {
        return gender;
    }

    public void setGender(Short gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public User() {
    }

    public User(Integer id, String name, Short age, Short gender, String phone) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                ", gender=" + gender +
                ", phone='" + phone + ''' +
                '}';
    }
}

Jackson 使用示例

添加依赖

如果你使用 Maven,可以在 pom.xml 中添加 Jackson 的依赖:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>
对象转 JSON 字符串
public static void main(String[] args) throws JsonProcessingException {
    User user = new User(1, "jack", (short) 18, (short) 1, "13888888888");

    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(user);
    System.out.println(json);
}

image.png

JSON 字符串转对象
public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = "{"name":"李四","age":25}";

    // 将 JSON 字符串转换为对象
    User user = objectMapper.readValue(jsonString, User.class);
    System.out.println(user.getName()); // 输出:李四
    System.out.println(user.getAge());  // 输出:25
}

image.png

Fastjson 使用示例

添加依赖

同样,如果你使用 Maven,可以在 pom.xml 中添加 Fastjson 的依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.83</version>
</dependency>
对象转 JSON 字符串
public static void main(String[] args) throws JsonProcessingException {
    User user = new User();
    user.setName("王五");
    user.setAge((short) 28);
    String jsonString = JSON.toJSONString(user);
    System.out.println(jsonString);
}

image.png

序列化为 JSON 对象
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Example {
    public static void main(String[] args) {
        // 创建一个简单的Java对象
        Person person = new Person("Alice", 30);
        
        // 将Java对象序列化为JSONObject
        JSONObject jsonObject = (JSONObject) JSON.toJSON(person);
        System.out.println(jsonObject);
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters and setters are required for Fastjson to work correctly.
    // Make sure you have getters and setters in your class.
}
从字符串创建 JSONObject
public static void main(String[] args) throws JsonProcessingException {
    String jsonString = "{"name":"李四","age":25}";
    JSONObject jsonObject = JSON.parseObject(jsonString);
    System.out.println(jsonObject.get("name"));
    System.out.println(jsonObject.get("age"));
}

image.png

JSON 字符串转对象,指定对象
public static void main(String[] args) throws JsonProcessingException {
    String jsonString = "{"name":"赵六","age":35}";
    // 将 JSON 字符串转换为对象
    User user = JSON.parseObject(jsonString, User.class);
    System.out.println(user.getName()); // 输出:赵六
    System.out.println(user.getAge());  // 输出:35
}

image.png

JSONObject 常用用法

 org.json 库中的 JSONObject

创建 JSONObject
public static void main(String[] args) throws JsonProcessingException {
    JSONObject json =  new JSONObject();
    json.put("name","wjw");
    json.put("age",18);
    System.out.println(json);
}
从字符串创建 JSONObject
import org.json.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        String jsonString = "{"name":"李四","age":25}";

        // 从 JSON 字符串创建 JSONObject
        JSONObject jsonObject = new JSONObject(jsonString);

        // 获取键值
        String name = jsonObject.getString("name");
        int age = jsonObject.getInt("age");

        // 输出结果
        System.out.println(name); // 输出:李四
        System.out.println(age);  // 输出:25
    }
}
修改 JSONObject
import org.json.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "王五");
        jsonObject.put("age", 28);

        // 修改键值
        jsonObject.put("age", 30);

        // 输出结果
        System.out.println(jsonObject.toString()); // 输出:{"name":"王五","age":30}
    }
}
删除键值对
import org.json.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "赵六");
        jsonObject.put("age", 35);

        // 删除键值对
        jsonObject.remove("age");

        // 输出结果
        System.out.println(jsonObject.toString()); // 输出:{"name":"赵六"}
    }
}

Fastjson 库中的 JSONObject

创建 JSONObject
import com.alibaba.fastjson.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        // 创建一个空的 JSONObject
        JSONObject jsonObject = new JSONObject();

        // 向 JSONObject 中添加键值对
        jsonObject.put("name", "张三");
        jsonObject.put("age", 30);

        // 输出 JSONObject
        System.out.println(jsonObject.toJSONString()); // 输出:{"name":"张三","age":30}
    }
}
从字符串创建 JSONObject
import com.alibaba.fastjson.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        String jsonString = "{"name":"李四","age":25}";

        // 从 JSON 字符串创建 JSONObject
        JSONObject jsonObject = JSONObject.parseObject(jsonString);

        // 获取键值
        String name = jsonObject.getString("name");
        int age = jsonObject.getIntValue("age");

        // 输出结果
        System.out.println(name); // 输出:李四
        System.out.println(age);  // 输出:25
    }
}
修改 JSONObject
import com.alibaba.fastjson.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "王五");
        jsonObject.put("age", 28);

        // 修改键值
        jsonObject.put("age", 30);

        // 输出结果
        System.out.println(jsonObject.toJSONString()); // 输出:{"name":"王五","age":30}
    }
}
删除键值对
import com.alibaba.fastjson.JSONObject;

public class JSONObjectExample {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "赵六");
        jsonObject.put("age", 35);

        // 删除键值对
        jsonObject.remove("age");

        // 输出结果
        System.out.println(jsonObject.toJSONString()); // 输出:{"name":"赵六"}
    }
}