使用resttemplate解析JSON数组

时间:2016-03-06 00:14:38

标签: arrays json jackson mapping resttemplate

我认为我有一个非常常见的问题,但我无法找到解决方案:(

我使用spring和restTemplate来恢复这样的JSON对象:

ResponseEntity<Download_urls> result= restTemplate.exchange(URL, HttpMethod.GET, entity, Download_urls.class);

Where&#34; Download_urls&#34; class里面有一个JSON数组:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)

public class Download_urls {    

    private Video[] video;

}

和Video.class

@JsonIgnoreProperties(ignoreUnknown = true)
public class Video {

    private String type;
    private String label;
    private String file;

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getLabel() {
        return label;
    }
    public void setLabel(String label) {
        this.label = label;
    }
    public String getFile() {
        return file;
    }
    public void setFile(String file) {
        this.file = file;
    }

}

显然,Video []无法映射JSON数组。有什么帮助吗?

由于

更新 示例JSON有效负载:

{
    "id": 737132,
    "asset": {
        "_class": "asset",
        "id": 538362,
        "download_urls": {
            "Video": [{
                "type": "video/mp4",
                "label": "360"
            }, {
                "type": "video/mp4",
                "label": "720"
            }]
        }
    }
}

2 个答案:

答案 0 :(得分:0)

解决!!

是我的:

private Video[] video;

尝试使用public属性并且有效:

public Video[] video;

答案 1 :(得分:0)

您的Java类名称及其属性应遵循Java命名约定。然后你的代码更具可读性和更好。要转换JSON字段名称,您可以使用命名策略,例如:LowerCaseWithUnderscoresStrategyPascalCaseStrategy等。

这里你是我的课程应该是这样的:

Video.java - 和你的一样。

DownloadUrls.java:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.PascalCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonIgnoreProperties(ignoreUnknown = true)

// PascalCaseStrategy is used here because of "Video" JSON field. I would expect
// it to be called "video".
@JsonNaming(PascalCaseStrategy.class)
public class DownloadUrls {

    private Video[] video;

    public Video[] getVideo() {
        return video;
    }
    public void setVideo(Video[] video) {
        this.video = video;
    }
}

Asset.java:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonIgnoreProperties(ignoreUnknown = true)

// LowerCaseWithUnderscoresStrategy is common strategy when used with JSON, but
// in this case it is used because of "download_url" JSON field only.
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class Asset {

    int id;
    DownloadUrls downloadUrls;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public DownloadUrls getDownloadUrls() {
        return downloadUrls;
    }
    public void setDownloadUrls(DownloadUrls downloadUrls) {
        this.downloadUrls = downloadUrls;
    }   
}

和外部类型只是为了完整起见:

public class OuterType {

    int id;
    Asset asset;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public Asset getAsset() {
        return asset;
    }
    public void setAsset(Asset asset) {
        this.asset = asset;
    }
}

米甲