如何将JSON转换为Java对象?

Dhio Putro:

我是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对象。任何想法如何解决这个问题?

Mojtaba jalambadani:

用户ObjectMapper

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

检查此链接

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章