2011-09-25 74 views
2

擊中twitter friends/ids API端點,而無需指定將返回該JSON響應光標:指定你的文檔格式的遊標時使用傑克遜映射一個無名數組

["243439460","13334762", "14654522"]

但是:

{"next_cursor":0,"previous_cursor":0,"ids":["243439460","13334762","14654522"]}

使用傑克遜反序列化的第二個反應很容易使用

@JsonIgnoreProperties(ignoreUnknown = true) 
public class FriendIds { 
private List<String> ids; 

public List<String> getIds() { return ids; } 

public void setIds(List<String> ids) { this.ids = ids; } 
} 

FriendIds friendIds = new ObjectMapper().readValue(jsonStr, FriendIds.class);

但是我還沒有發現類似的方式來使用deserialise傑克遜FriendIds的第一反應。任何想法如何做到這一點?

+0

第一反應不是JSON - 它只是一個數組 –

+0

@IgorDymov是的,它是。我最初認爲,但有史以來,我把它扔在接受它。 http://jsonlint.com/ – markdsievers

+2

它是有效的JSON,沒問題,但作爲一個數組,它需要映射到一個數組或集合.... – StaxMan

回答

4

如何只將它們映射到ID列表第一,如:

List<String> ids = mapper.readValue(json, List.class); // works because String happens to be 'natural' type for JSON Strings 

,然後就構建包裝對象?

Jackson沒有真正的方法知道JSON數組將被用於構造POJO,其中一個字段包含列表的內容,除了編寫自定義解串器。 如果你想這樣做,這是一種可能性;事實上可以支持這兩種情況。但需要一些思考,爲JSON對象使用一種方法(默認POJO deser),另一種用於JSON數組。 因此,在傑克遜之外這可能是最容易做到的。

2

我希望這有助於。您可以使用Spring RestTemplate.getForObject和Jackson註釋來完成此操作。

這是微博響應的類:

import java.util.List; 

import com.fasterxml.jackson.annotation.JsonCreator; 
import com.fasterxml.jackson.annotation.JsonProperty; 

public class TwitterResponse { 
    private List<TwitterId> ids; 

    @JsonCreator 
    public TwitterResponse(List<TwitterId> ids) { 
    this.ids = ids; 
    } 

    public List<TwitterId> getIds() { 
    return ids; 
    } 

    @Override 
    public String toString() { 
    return "TwitterResponse [ids=" + ids + "]"; 
    } 

} 

這是TwitterId類:

import com.fasterxml.jackson.annotation.JsonProperty; 

public class TwitterId { 

    private String twitterId; 

    public TwitterId(String twitterId) { 
    this.twitterId = twitterId; 
    } 

    public String getTwitterId() { 
    return twitterId; 
    } 

    @Override 
    public String toString() { 
    return "TwitterId [twitterId=" + twitterId + "]"; 
    } 

} 

和一個簡單的單元測試示出它的工作原理。我剛剛創建一個簡單的控制器,返回上述數據,以及我使用RestTemplate

import org.junit.Test; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.web.client.RestTemplate; 

public class ControllerTest { 
    private static final Logger log = LoggerFactory.getLogger(ControllerTest.class); 
@Test 
    public void testTwitter() { 
    try { 
     RestTemplate restTemplate = new RestTemplate(); 

     TwitterResponse twitterResponse = 
      restTemplate.getForObject("http://localhost:8080/twitter", 
       TwitterResponse.class); 

     log.debug("twitterResponse = {}", twitterResponse); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    } 

} 

和控制檯顯示測試結果測試它:

19:57:42.506 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8080/twitter" 
19:57:42.545 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json] 
19:57:42.558 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8080/twitter" resulted in 200 (OK) 
19:57:42.559 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class org.porthos.simple.TwitterResponse] as "application/json;charset=UTF-8" using [org.springfr[email protected]4b5a5ed1] 
19:57:42.572 [main] DEBUG org.porthos.simple.ControllerTest - twitterResponse = TwitterResponse [ids=[TwitterId [twitterId=243439460], TwitterId [twitterId=13334762], TwitterId [twitterId=14654522]]]