2015-04-28 33 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].

相关问题