简体   繁体   English

如何将JSON转换为Java object?

[英]How to convert JSON to Java object?

I'm new to JSON and I have trouble converting JSON to Java object. Here is the JSON I want to convert.我是 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
      }
  }
} 

This the class that I have made.这是我制作的 class。 I might be wrong but is it okay to use the HashMap to map the grades key in the JSON?我可能错了,但是可以使用 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;
    }
}

I want to retrieve each key:value and convert them to Java object with the class I have made.我想检索每个键:值并将它们转换为 Java object 我制作的 class 。 Any idea how to resolve this?知道如何解决这个问题吗?

user ObjectMapper用户对象映射器

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

Check This Link 检查此链接

Using the org.json library, this is how you retrieve your objects.使用 org.json 库,这就是您检索对象的方式。 Just use the key iterator `JSONObject.keys()' to iterate through and retrieve all keys and pass the retrieval to your constructor.只需使用键迭代器“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 objects are quite simple when you get your head around iterating.当您开始迭代时,JSON 对象非常简单。

Output of the code will be: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM