-5

I have the following JSON text. How can I parse it to get response-code, response, result, DISPLAYNAME ,AVAILABILITYSEVERITY, RESOURCEID , ETC?

{ "response-code":"4000", "response": { "result": [ { "DISPLAYNAME":"Backup Server", "AVAILABILITYSEVERITY":"5", "RESOURCEID":"10002239110", "TYPE":"SUN", "SHORTMESSAGE":"Clear" } ] ,"uri":"/json/ListAlarms" } }

1 Answers1

0
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...

shahaf
  • 4,750
  • 2
  • 29
  • 32
  • JSON result not fixed(data coming from server) and i need all JSON variable on output – Firuzzadeh May 13 '18 at 18:54
  • @TanintecFiruzzadeh what do you mean by "not fixed", this is an example to get the "response-code" field, you can do it to get all other fields – shahaf May 13 '18 at 19:23
  • i get JSON data form Alarm server(Alarm server generated Json data) , and i need more output Excepted response-code – Firuzzadeh May 13 '18 at 21:17
  • i need below output response-code=4000 DISPLAYNAME=Backup Server AVAILABILITYSEVERITY=5 RESOURCEID:10002239110 TYPE=SUN SHORTMESSAGE=Clear – Firuzzadeh May 13 '18 at 21:20
  • as I said to get other values it's pretty straight forward – shahaf May 13 '18 at 22:09