Parse JSON array with resttemplate

风格不统一 提交于 2019-12-24 15:31:48

问题


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

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

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;

}

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

}

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

Thanks

UPDATE: Example JSON payload:

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

回答1:


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 - same as yours.

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

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

Michal




回答2:


Solved!!

It was my:

private Video[] video;

tried with public attribute and it works:

public Video[] video;


来源:https://stackoverflow.com/questions/35821534/parse-json-array-with-resttemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!