Java Reference
In-Depth Information
The following code illustrates how to do this:
package com.ensode.jsonpmodelapi;
//imports omitted
@Named
@RequestScoped
public class JsonPModelApiBean {
@Inject
private Person person;
private String jsonStr;
public String generateJson() {
//body omitted for brevity
}
public String parseJson() {
JsonObject jsonObject;
try (JsonReader jsonReader = Json.createReader(
new StringReader(jsonStr))) {
jsonObject = jsonReader.readObject();
}
person.setFirstName(jsonObject.getString("firstName"));
person.setMiddleName(jsonObject.getString(
"middleName"));
person.setLastName(jsonObject.getString("lastName"));
person.setGender(jsonObject.getString("gender"));
person.setAge(jsonObject.getInt("age"));
return "display_populated_obj";
}
public String getJsonStr() {
return jsonStr;
}
public void setJsonStr(String jsonStr) {
this.jsonStr = jsonStr;
}
}
 
Search WWH ::




Custom Search