如何在Java中解析JSON Text

时间:2018-05-13 10:22:46

标签: java json parsing

我有以下JSON文本。如何解析它以获取响应代码,响应,结果,DISPLAYNAME,AVAILABILITYSEVERITY,RESOURCEID,ETC?

{ “响应代码”:“4000”, “响应”: { “结果”: [ { “DISPLAYNAME”:“备份服务器”, “AVAILABILITYSEVERITY”: “5”, “RESOURCEID”: “10002239110”, “类型”: “SUN”, “短消息”:“清除” } ] “URI”: “/ JSON / ListAlarms” } }

1 个答案:

答案 0 :(得分: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();
    }

}

输出

"4000"
"Backup Server"
"5"
"10002239110"
"SUN"
"Clear"
"/json/ListAlarms"

另一种方法如果json具有固定结构,则构建对象以表示json结构,并使用jackson将json转换为java对象,如

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;
    }
}

然后,访问类似的值

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());

输出相同......