使用resttemplate解析JSON数组 [英] Parse JSON array with resttemplate

查看:366
本文介绍了使用resttemplate解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我有一个非常普遍的问题,但是我找不到解决方法:(

I think I have a very common problem but I cant find a solution :(

我正在将spring与restTemplate一起使用来恢复这样的JSON对象:

I am using spring with restTemplate to recover a JSON object like this:

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

其中"Download_urls"类内部具有JSON数组:

Where "Download_urls " class have a JSON array inside:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)

public class Download_urls {    

    private Video[] video;

}

和Video.class

And 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数组.有帮助吗?

Obviously Video[] doesn't work to map JSON array. Any help?

谢谢

更新: JSON有效负载示例:

UPDATE: Example JSON payload:

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

推荐答案

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

Your Java class names and its properties should follow Java naming conventions. Then your code is much more readable and nicer. And to convert JSON field names to and fro you can use naming strategies, e.g.: LowerCaseWithUnderscoresStrategy, PascalCaseStrategy, etc.

在这里,我就是我的班级样子:

Here you are how I thing classes should look like:

Video.java-与您的相同.

Video.java - same as yours.

DownloadUrls.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:

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;
    }   
}

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

and outer type just for completeness sake:

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;
    }
}

米哈尔

这篇关于使用resttemplate解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆