import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public static void main(String[] args) {
final String json = "{ \"response-code\":\"4000\", \"response\": { \"result\": [ { \"DISPLAYNAME\":\"Backup Server\", \"AVAILABILITYSEVERITY\":\"5\", \"RESOURCEID\":\"10002239110\", \"TYPE\":\"SUN\", \"SHORTMESSAGE\":\"Clear\" } ] ,\"uri\":\"/json/ListAlarms\" } }";
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode obj = mapper.readTree(json);
System.out.println(obj.get("response-code"));
JsonNode response = obj.get("response");
JsonNode firstResult = response.get("result").get(0);
System.out.println(firstResult.get("DISPLAYNAME"));
System.out.println(firstResult.get("AVAILABILITYSEVERITY"));
System.out.println(firstResult.get("RESOURCEID"));
System.out.println(firstResult.get("TYPE"));
System.out.println(firstResult.get("SHORTMESSAGE"));
System.out.println(response.get("uri"));
} catch (IOException e) {
e.printStackTrace();
}
}
output
"4000"
"Backup Server"
"5"
"10002239110"
"SUN"
"Clear"
"/json/ListAlarms"
another approach only if the json has fixed structure, is to build objects to represent the json structure and use jackson
to cast that json to a java object, like
class JsonObj {
@JsonProperty("response-code")
private long responseCode;
private ResponseObj response;
public long getResponseCode() {
return responseCode;
}
public void setResponseCode(long responseCode) {
this.responseCode = responseCode;
}
public ResponseObj getResponse() {
return response;
}
public void setResponse(ResponseObj response) {
this.response = response;
}
}
class ResponseObj {
private ArrayList<ResultObj> result;
private String uri;
public ArrayList<ResultObj> getResult() {
return result;
}
public void setResult(ArrayList<ResultObj> result) {
this.result = result;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
}
class ResultObj {
@JsonProperty("DISPLAYNAME")
private String displayName;
@JsonProperty("TYPE")
private String type;
@JsonProperty("AVAILABILITYSEVERITY")
private int availabilitySeverity;
@JsonProperty("RESOURCEID")
private String resourceId;
@JsonProperty("SHORTMESSAGE")
private String shortMessage;
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAvailabilitySeverity() {
return availabilitySeverity;
}
public void setAvailabilitySeverity(int availabilitySeverity) {
this.availabilitySeverity = availabilitySeverity;
}
public String getResourceId() {
return resourceId;
}
public void setResourceId(String resourceId) {
this.resourceId = resourceId;
}
public String getShortMessage() {
return shortMessage;
}
public void setShortMessage(String shortMessage) {
this.shortMessage = shortMessage;
}
}
and then, access the values like that
JsonObj jsonObj = mapper.readValue(json, JsonObj.class);
System.out.println(jsonObj.getResponseCode());
ResponseObj response = jsonObj.getResponse();
ResultObj firstResult = response.getResult().get(0);
System.out.println(firstResult.getDisplayName());
System.out.println(firstResult.getAvailabilitySeverity());
System.out.println(firstResult.getResourceId());
System.out.println(firstResult.getType());
System.out.println(firstResult.getShortMessage());
System.out.println(response.getUri());
the output is the same...