You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JSON-Java(org.json)库将JSON反序列化为POJO

使用JSON-Java(org.json)将JSON反序列化为POJO

首先要明确:org.json库不会自动映射字段名,POJO的字段名必须和JSON的键名严格匹配,你的原POJO存在字段名不匹配的问题,需要先修正:

  • JSON中batters下的数组键是batter,但原POJO的Batters类字段是bats,需改为batter
  • JSON中的数组键是topping,原POJO的字段是toppings,需改为topping

修正后的POJO类

import java.util.List;

public class Donut {
    private String id;
    private String type;
    private String name;
    private double ppu;
    private Batters batters;
    private List<Topping> topping;

    // Getters and Setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getPpu() { return ppu; }
    public void setPpu(double ppu) { this.ppu = ppu; }
    public Batters getBatters() { return batters; }
    public void setBatters(Batters batters) { this.batters = batters; }
    public List<Topping> getTopping() { return topping; }
    public void setTopping(List<Topping> topping) { this.topping = topping; }

    public static class Batters {
        private List<Batter> batter;

        // Getters and Setters
        public List<Batter> getBatter() { return batter; }
        public void setBatter(List<Batter> batter) { this.batter = batter; }

        public static class Batter {
            private String id;
            private String type;

            // Getters and Setters
            public String getId() { return id; }
            public void setId(String id) { this.id = id; }
            public String getType() { return type; }
            public void setType(String type) { this.type = type; }
        }
    }

    public static class Topping {
        private String id;
        private String type;

        // Getters and Setters
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
    }
}

反序列化实现代码

org.json需要手动逐层解析JSON结构,再给POJO的字段赋值,示例代码如下:

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class JsonDeserializer {
    public static void main(String[] args) {
        // 待解析的JSON字符串
        String jsonStr = "{\"id\": \"0001\",\"type\": \"donut\",\"name\": \"Cake\",\"ppu\": 0.55,\"batters\":{\"batter\":[{ \"id\": \"1001\", \"type\": \"Regular\" },{ \"id\": \"1002\", \"type\": \"Chocolate\" }]},\"topping\":[{ \"id\": \"5001\", \"type\": \"None\" },{ \"id\": \"5002\", \"type\": \"Glazed\" },{ \"id\": \"5005\", \"type\": \"Sugar\" }]}";

        // 解析为JSONObject
        JSONObject jsonObject = new JSONObject(jsonStr);

        // 初始化Donut对象
        Donut donut = new Donut();
        donut.setId(jsonObject.getString("id"));
        donut.setType(jsonObject.getString("type"));
        donut.setName(jsonObject.getString("name"));
        donut.setPpu(jsonObject.getDouble("ppu"));

        // 解析Batters嵌套结构
        JSONObject battersJson = jsonObject.getJSONObject("batters");
        Donut.Batters batters = new Donut.Batters();
        JSONArray batterArray = battersJson.getJSONArray("batter");
        List<Donut.Batters.Batter> batterList = new ArrayList<>();

        for (int i = 0; i < batterArray.length(); i++) {
            JSONObject batterJson = batterArray.getJSONObject(i);
            Donut.Batters.Batter batter = new Donut.Batters.Batter();
            batter.setId(batterJson.getString("id"));
            batter.setType(batterJson.getString("type"));
            batterList.add(batter);
        }
        batters.setBatter(batterList);
        donut.setBatters(batters);

        // 解析Topping数组
        JSONArray toppingArray = jsonObject.getJSONArray("topping");
        List<Donut.Topping> toppingList = new ArrayList<>();

        for (int i = 0; i < toppingArray.length(); i++) {
            JSONObject toppingJson = toppingArray.getJSONObject(i);
            Donut.Topping topping = new Donut.Topping();
            topping.setId(toppingJson.getString("id"));
            topping.setType(toppingJson.getString("type"));
            toppingList.add(topping);
        }
        donut.setTopping(toppingList);

        // 测试输出
        System.out.println("Donut ID: " + donut.getId());
        System.out.println("First batter type: " + donut.getBatters().getBatter().get(0).getType());
        System.out.println("Last topping type: " + donut.getTopping().get(donut.getTopping().size()-1).getType());
    }
}

注意事项

  • 确保已引入org.json依赖(Maven可添加<dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20240303</version></dependency>)
  • 所有字段的类型要和JSON中的值类型匹配,比如ppu是double类型,JSON中是数字0.55,用getDouble()获取
  • 数组类型需要遍历JSONArray,逐个转换为对应的POJO对象,再存入List

内容的提问来源于stack exchange,提问作者Edwin Chiboko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 17:48:29