Java - Jackson JSON Library and ObjectMapper.readValue -
i have following json data (patients.json):
{ "a" : { "name" : "tom", "age" : 12 }, "b" : { "name" : "jim", "age" : 54 } }
using jackson json library, how can following:
hashmap<string, ???> patients = objectmapper.readvalue(new file("patients.json"), ???); string aname = patients.get("a").get("name"); int aname = patients.get("a").get("age");
you create class map patients to;
private static class patient { @jsonproperty("name") private string name; @jsonproperty("age") private int age; public patient() { } public string getname() { return name; } public int getage() { return age; } }
then read json via jackson
hashmap<string, patient> patients = objectmapper.readvalue(new file("patients.json"), new typereference<hashmap<string,patient>>() {}); patient patienta = patients.get("a"); string patientaname = patienta.getname(); int pateintaage = patienta.getage();
Comments
Post a Comment