【问题标题】:How to convert JSON to Java object?如何将 JSON 转换为 Java 对象?
【发布时间】:2020-07-18 03:46:30
【问题描述】:

我是 JSON 新手,我无法将 JSON 转换为 Java 对象。 这是我要转换的 JSON。

{ 
  "John" : {
    "fullname"  : "John Wick",
    "address"   : "New York",
    "status"    : "Active",
    "grades" : {
        "physics"   : 80,
        "calculus"  : 70,
        "biology"   : 85
      }
  },
  "Indie" : {
    "fullname"  : "Indiana Jones",
    "address"   : "Los Angeles",
    "status"    : "On Leave",
    "grades" : {
        "physics"   : 75,
        "calculus"  : 95,
        "biology"   : 65
      }
  },
  "Gerard" : {
    "fullname"  : "Gerard Butler",
    "address"   : "San Fransisco",
    "status"    : "Non Active",
    "grades" : {
        "physics"   : 0,
        "calculus"  : 0,
        "biology"   : 0
      }
  }
} 

这是我制作的课程。我可能错了,但是可以使用 HashMap 来映射 JSON 中的 Grades 键吗?

public class Student {
    private long id;
    private String name;
    private String address;
    private String status;
    private HashMap<String, Integer> score;


    public Siswa(String name, String address, String status, HashMap<String, Integer> score) {
        this.score = score;
        this.name = name;
        this.address = address;
        this.status = status;
    }

    public Student(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getAddress() {
        return this.address;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getStatus() {
        return this.status;
    }

    public HashMap<String, Integer> getScore() {
        return score;
    }
    public void setNilai(HashMap<String, Integer> score) {
        this.score = score;
    }

}

class Score {
    private int id_score;
    private int phyGrade;
    private int calGrade;
    private int bioGrade;

    public Nilai(int id_score, int phyGrade, int calGrade, int bioGrade) {
        this.id_score = id_score;
        this.phyGrade = phyGrade;
        this.calGrade = calGrade;
        this.bioGrade = bioGrade;
    }

    public int getId_score() {
        return id_score;
    }

    public void setId_nilai(int id_nilai) {
        this.id_score = id_score;
    }
    public void setPhyGrade(int phy) {
        this.phyGrade = phy;
    }
    public int getPhyGrade() {
        return this.phyGrade;
    }
    public void setCalGrade(int cal) {
        this.calGrade = cal;
    }
    public int getCalGrade() {
        return this.calGrade;
    }
    public void setBioGrade(int bio) {
        this.bioGrade = bio;
    }
    public int getBioGrade() {
        return this.bioGrade;
    }
}

我想检索每个 key:value 并使用我创建的类将它们转换为 Java 对象。 知道如何解决这个问题吗?

【问题讨论】:

  • 也许可以看看谷歌的 gson 库。
  • 这能回答你的问题吗? Converting JSON data to Java object
  • 你可以使用这个jsonschema2pojo.org进行JSON到JAVA类的转换
  • 您可以使用地图界面来存储学生的成绩。这样一来,无论您使用 HashMap 还是任何其他 Map 实现都无关紧要。

标签: java json object


【解决方案1】:

用户对象映射器

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

Check This Link

【讨论】:

  • 这是用杰克逊吗?我以前见过,但想不起来了。还是谢谢
【解决方案2】:

使用 org.json 库,这是您检索对象的方式。只需使用键迭代器 `JSONObject.keys()' 来迭代并检索所有键并将检索传递给您的构造函数。

     String jsonReturn = "{ \n" +
                "  \"John\" : {\n" +
                "    \"fullname\"  : \"John Wick\",\n" +
                "    \"address\"   : \"New York\",\n" +
                "    \"status\"    : \"Active\",\n" +
                "    \"grades\" : {\n" +
                "        \"physics\"   : 80,\n" +
                "        \"calculus\"  : 70,\n" +
                "        \"biology\"   : 85\n" +
                "      }\n" +
                "  },\n" +
                "  \"Indie\" : {\n" +
                "    \"fullname\"  : \"Indiana Jones\",\n" +
                "    \"address\"   : \"Los Angeles\",\n" +
                "    \"status\"    : \"On Leave\",\n" +
                "    \"grades\" : {\n" +
                "        \"physics\"   : 75,\n" +
                "        \"calculus\"  : 95,\n" +
                "        \"biology\"   : 65\n" +
                "      }\n" +
                "  },\n" +
                "  \"Gerard\" : {\n" +
                "    \"fullname\"  : \"Gerard Butler\",\n" +
                "    \"address\"   : \"San Fransisco\",\n" +
                "    \"status\"    : \"Non Active\",\n" +
                "    \"grades\" : {\n" +
                "        \"physics\"   : 0,\n" +
                "        \"calculus\"  : 0,\n" +
                "        \"biology\"   : 0\n" +
                "      }\n" +
                "  }\n" +
                "} ";



        JSONObject jsonObject = new JSONObject(jsonReturn);
        
        System.out.println(jsonObject.keySet());
        
        JSONObject John = (JSONObject) jsonObject.get("John");
        
        System.out.println("JSON OBJECT: " + John);
        System.out.println("Johns address: " + John.get("address"));
        System.out.println("Johns grades: " + John.get("grades"));

当您了解迭代时,JSON 对象非常简单。

代码的输出将是:

[Indie, Gerard, John]

JSON OBJECT: {"address":"New York","fullname":"John Wick","grades": 
"biology":85,"physics":80,"calculus":70},
"status":"Active"}

 Johns address: New York

 Johns grades: {"biology":85,"physics":80,"calculus":70}

【讨论】:

  • 非常有帮助的人,非常感谢。着手解决这个问题。
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多