【问题标题】:Parse JSON array with resttemplate使用 resttemplate 解析 JSON 数组
【发布时间】:2016-06-19 16:37:11
【问题描述】:

我想我有一个很常见的问题,但我找不到解决方案:(

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

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

“Download_urls”类里面有一个 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"
            }]
        }
    }
}

【问题讨论】:

  • 您能否发布一个示例 JSON 有效负载?
  • 当然@MichalFoksa !!是这样的: {"id": 737132, "asset": { "_class": "asset", "id": 538362, "download_urls": { "Video": [ {"type": "video/mp4 ", "label": "360"}, {"type": "video/mp4", "label": "720"}]}}} 我无法从 download_url 检索视频 JSON 数组 谢谢!
  • 你定义了资产类及其外部类型(带有id和资产)吗?
  • 是的,一切顺利,直到我尝试调试 Download_url 类时 Video 类变为 null。
  • 从 Download_url 类中移除 @JsonIgnoreProperties(ignoreUnknown = true)。这会给你有用的信息什么是错的。

标签: arrays json jackson mapping resttemplate


【解决方案1】:

您的 Java 类名及其属性应遵循 Java 命名约定。这样你的代码就更易读、更漂亮了。要来回转换 JSON 字段名称,您可以使用命名策略,例如:LowerCaseWithUnderscoresStrategyPascalCaseStrategy 等。

这是我的东西类应该是这样的:

Video.java - 和你的一样。

下载Urls.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;
    }
}

资产.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;
    }
}

迈克尔

【讨论】:

    【解决方案2】:

    解决了!!

    这是我的:

    private Video[] video;
    

    用公共属性试过了,效果很好:

    public Video[] video;
    

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2014-08-14
      • 2013-03-21
      相关资源
      最近更新 更多