我当前正在使用杰克逊gwt库,我想知道如何访问嵌套在json字符串中的值。我当前的设置代码如下:
public static interface PremiumMapper extends ObjectMapper<Premium> {}
public static class Premium {
public float getFaceAmount() {
return faceAmount;
}
public void setFaceAmount(float faceAmount) {
this.faceAmount = faceAmount;
}
public float getStart() {
return start;
}
public void setStart(float start) {
this.start = start;
}
public float getEnd() {
return end;
}
public void setEnd(float end) {
this.end = end;
}
public float start;
public float end;
public float faceAmount;
}
我的访问代码如下:
String json = "{\"errorCode\":\"validation.failed\",\"statusCode\":400,\"details\":[{\"code\":\"targetPremium.monthlyPremiumsCents.outOfBounds\",\"validChoices\":[{\"start\":9450,\"end\":125280}]}]}";
PremiumMapper mapper = GWT.create( PremiumMapper.class );
Premium premium = mapper.read( json );
System.out.println("VALUES OUTPUTTED: " + premium.getStart());
如何获取json字符串中的起始值?
答案 0 :(得分:2)
尝试将此作为您的高级课程:
-----------------------------------com.example.Premium.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Premium {
private String errorCode;
private Integer statusCode;
private List<Detail> details = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Detail.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Detail {
private String code;
private List<ValidChoice> validChoices = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<ValidChoice> getValidChoices() {
return validChoices;
}
public void setValidChoices(List<ValidChoice> validChoices) {
this.validChoices = validChoices;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.ValidChoice.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.Map;
public class ValidChoice {
private Integer start;
private Integer end;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getEnd() {
return end;
}
public void setEnd(Integer end) {
this.end = end;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
并获得如下起始值:premium.getDetails().get(0).getValidChoices(0).getStart()