gpt4 book ai didi

java - 使用 Jackson GWT 访问嵌套 JSON 值

转载 作者:行者123 更新时间:2023-11-30 01:58:21 24 4
gpt4 key购买 nike

我目前正在使用 jackson 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 字符串中的起始值?

最佳答案

尝试将此作为您的高级类(class):

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

关于java - 使用 Jackson GWT 访问嵌套 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53674130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com