[英]How to convert JSON to Java object?
我是 JSON 的新手,我无法将 JSON 转换为 Java object。这是我要转换的 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
}
}
}
这是我制作的 class。 我可能错了,但是可以使用 HashMap 到 map 中的成绩键 JSON 吗?
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;
}
}
我想检索每个键:值并将它们转换为 Java object 我制作的 class 。 知道如何解决这个问题吗?
用户对象映射器
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);
使用 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 对象非常简单。
Output 的代码将是:
[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}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.