In the last article, we saw how to generate JSON data from our Java code with the Model API. In this section, we will see how we can read and parse the existing JSON data. The following code sample illustrates how to do this:
importimportjavax.json.Json;
importimportjavax.json.JsonObject;
importimportjavax.json.JsonReader;
importimportjavax.json.JsonWriter;
@Named
@SessionScoped
public class JsonpBean implements Serializable {
private String jsonStr;
@Inject private Customer customer;
public void parseJson() {
JsonObjectjsonObject;
try (JsonReader jsonReader = Json.createReader( new StringReader(jsonStr))) {
jsonObject = jsonReader.readObject();
}
customer.setFirstName(jsonObject.getString("firstName"));
customer.setLastName(jsonObject.getString("lastName"));
customer.setEmail(jsonObject.getString("email"));
}
}
No comments:
Post a Comment