如何使用GSON

时间:2017-11-16 12:31:16

标签: java json spring-boot gson json-deserialization

JSON

    "entities": {
        "nca_location": [
            {
                "confidence": 0.85126983589896,
                "value": "struga",
                "type": "value"
            },
            {
                "confidence": 0.9188255680843,
                "value": "Skopje",
                "type": "value"
            }
        ],
        "intent": [
            {
                "confidence": 0.99990092463824,
                "value": "nca_get_position_availability"
            }
        ]
    }

EntityDto

public class EntityDto {
    private String name;
    private List<EntityValueDto> values;

    public EntityDto(String name, List<EntityValueDto> values) {
        this.name = name;
        this.values = values;
    }

    public List<EntityValueDto> getValues() {
        return values;
    }

    public void setValues(List<EntityValueDto> values) {
        this.values = values;
    }
}

EntityValueDto

public class EntityValueDto {
    private Float confidence;
    private String value;

    public EntityValueDto(Float confidence, String value) {
        this.confidence = confidence;
        this.value = value;
    }

    public Float getConfidence() {
        return confidence;
    }

    public void setConfidence(Float confidence) {
        this.confidence = confidence;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

如何使用GSON / Jackson自动进行这种映射?我想只提取实体值并从中列出一个列表。

EntityDto示例:nca_location将是名称,列表将是已解析的对象。

ResponseEntity response = restTemplate.exchange(requestUrl, HttpMethod.POST, entity, Object.class);

这是我从API获取响应对象的方式,我不知道如何继续。

0 个答案:

没有答案