2016-03-06 90 views
1

我想我有一个非常普遍的问题,但我不能找到一个解决办法:(解析JSON阵列resttemplate

我使用的弹簧,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数组。任何帮助?

由于

UPDATE: 例JSON有效载荷:

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

能否请您张贴示例JSON负载? –

+0

当然@MichalFoksa !!是这样的一个: { “ID”:737132, “资产”:{ “_class”: “资产”, “ID”:538362, “download_urls”:{ “视频”:[ { “type”:“video/mp4”,“label”:“360”}, {“type”:“video/mp4”,“label”:“720”}]}}} 我无法检索Video JSON数组from download_url 谢谢! –

+0

您是否定义了资产类别及其外部类型(包含ID和资产)? –

回答

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

米哈尔

0

解决!!

这是我的:

private Video[] video; 

试图与公共属性和它的作品:

public Video[] video;