2015-04-28 35 views
0

我有一個JSON有效載荷,看起來像這樣:如何使用Jackson解開JSON中的特定字段?

{ 
    "id": 32, 
    "name": "[Sample] Tomorrow is today, Red printed scarf", 
    "primary_image": { 
     "id": 247, 
     "zoom_url": "www.site.com/in_123__14581.1393831046.1280.1280.jpg", 
     "thumbnail_url": "www.site.com/in_123__14581.1393831046.220.290.jpg", 
     "standard_url": "www.site.com/in_123__14581.1393831046.386.513.jpg", 
     "tiny_url": "www.site.com/in_123__14581.1393831046.44.58.jpg" 
    } 
    } 

我可以解開一個特定的領域,放棄所有其他人呢?換句話說,我可以直接將其與POJO綁定:

public class Product { 

    private Integer id; 
    private String name; 
    private String standardUrl; 
} 

回答

2

有很多方法。你需要反序列化,序列化還是兩者?反序列化是使用一個創造者的方法,是以圖像作爲樹節點

方式一:

public static class Product { 
    private Integer id; 
    private String name; 
    private String standardUrl; 

    public Product(@JsonProperty("id") Integer id, 
        @JsonProperty("name") String name, 
        @JsonProperty("primary_image") JsonNode primaryImage) { 
     this.id = id; 
     this.name = name; 
     this.standardUrl = primaryImage.path("standard_url").asText(); 
    } 
} 

創建者不必是一個構造函數,你可以有一個是一個靜態方法僅用於Jackson的反序列化。

你必須定義一個自定義序列來重新序列化這一點,雖然(如StdDelegatingSerializer和轉換器包串備份作爲ObjectNode)

1

有皮膚不同的方式這隻貓,我希望你可以使用Jackson 2,因爲它提供了反序列化Json數據的好方法,我最喜歡的反序列化功能之一就是我在這裏給你看的一個(使用Builder Pattern),因爲它允許你在構造實例時驗證它(或使他們不可變!)。對你來說,這將是這樣的:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 

import java.util.Map; 

@JsonDeserialize(builder = Product.Builder.class) 
public class Product { 

private Integer id; 

private String name; 

private String standardUrl; 

private Product(Builder builder) { 
    //Here you can make validations for your new instance. 

    this.id = builder.id; 
    this.name = builder.name; 

    //Here you have access to the primaryImage map in case you want to add new properties later. 
    this.standardUrl = builder.primaryImage.get("standard_url"); 
} 

@Override 
public String toString() { 
    return String.format("id [%d], name [%s], standardUrl [%s].", id, name, standardUrl); 
} 

@JsonIgnoreProperties(ignoreUnknown = true) 
public static class Builder { 

    private Integer id; 

    private String name; 

    private Map<String, String> primaryImage; 

    public Builder withId(Integer id) { 
     this.id = id; 
     return this; 
    } 

    public Builder withName(String name) { 
     this.name = name; 
     return this; 
    } 

    @JsonProperty("primary_image") 
    public Builder withPrimaryImage(Map<String, String> primaryImage) { 
     this.primaryImage = primaryImage; 
     return this; 
    } 

    public Product build() { 
     return new Product(this); 
    } 
} 
} 

爲了測試它,我創建了這個類:

import com.fasterxml.jackson.databind.ObjectMapper; 

import java.io.IOException; 

public class Test { 
public static void main(String[] args) { 


    String serialized = "{" + 
         " \"id\": 32," + 
         " \"name\": \"[Sample] Tomorrow is today, Red printed scarf\"," + 
         " \"primary_image\": {" + 
         "  \"id\": 247," + 
         "  \"zoom_url\": \"www.site.com/in_123__14581.1393831046.1280.1280.jpg\"," + 
         "  \"thumbnail_url\": \"www.site.com/in_123__14581.1393831046.220.290.jpg\"," + 
         "  \"standard_url\": \"www.site.com/in_123__14581.1393831046.386.513.jpg\"," + 
         "  \"tiny_url\": \"www.site.com/in_123__14581.1393831046.44.58.jpg\"" + 
         " }" + 
         " }"; 


    ObjectMapper objectMapper = new ObjectMapper(); 

    try { 

     Product deserialized = objectMapper.readValue(serialized, Product.class); 

     System.out.print(deserialized.toString()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

輸出(使用覆蓋toString()方法Product

id [32], name [[Sample] Tomorrow is today, Red printed scarf], standardUrl [www.site.com/in_123__14581.1393831046.386.513.jpg].

相關問題