I have one Json String like below:
String empJson= {"id":"13480","name":"test","salary":29292"}
I want to convert above json string into Java object.
Emp.Java
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Emp {
@XmlElement(name="name")
private String m_name;
@XmlElement(name="id")
private String m_id;
@XmlElement(name="salary")
private String m_salary;
//setters and getters
}
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
JsonElement jElement = gson.fromJson(empJson, JsonElement.class);
System.out.println(jElement);
Emp = gson.fromJson(jElement, Emp.class);
System.out.println(" id : " + emp.getId());
But Emp object returning null values for all the fields.
Could you please help me on this.
Try after fixing input Json string
String empJson= {"id":"13480","name":"test","salary":"29292"}
By using ObjectMapper and JaxbAnnotationModule I have resolved my isssue.
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Emp emp = mapper.readValue(jsonObject.toString(), Emp.class);
System.out.println("EmpName: " + emp.getName());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.