如何使用Jackson在JSON中打开特定字段?

时间:2015-04-28 11:06:29

标签: java 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;
}

4 个答案:

答案 0 :(得分: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 :(得分:1)

有不同的方法可以使这只猫变亮,我希望你可以使用Jackson 2,因为它提供了很好的方法来反序列化Json数据,我最喜欢的反序列化功能之一是我将在这里展示的(使用{ {3}})因为允许你在构造实例时验证实例(或使它们不可变!)。对你来说,这将是这样的:

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].

答案 2 :(得分:0)

有两种方法可以获取所需的响应。对于这两种方法,我们将使用JsonView。

创建两种类型的JsonView:

public interface JViews {
    public static class Public { }
    public static class Product extends Public { }
}

第一种方法

@JsonView(JViews.Public.class)
public class Product {
    private Integer id;
    private String name;
    @JsonIgnore
    private Image primaryImage;
    @JsonView(JViews.Product.class)
    public String getStandardUrl{
       return this.primaryImage.getStandardUrl();
    }
}

第二种方式

一起使用Jackson的@JsonView和@JsonUnwrapped。

@JsonView(JViews.Public.class)
public class Product {
    private Integer id;
    private String name;

    @JsonUnwrapped
    private Image primaryImage;
}
public class Image {
   private String zoomUrl;
   @JsonView(JViews.Product.class)
   private String standardUrl;
}

@JsonUnwrapped注释将嵌套对象展平为Product对象。 JsonView用于过滤可访问字段。在这种情况下,“产品”视图只能访问standardUrl字段,结果应为:

{
    "id": 32,
    "name": "[Sample] Tomorrow is today, Red printed scarf",
    "standard_url": "url"
}

如果不使用View展平嵌套对象,结果将类似于:

{
    "id": 32,
    "name": "[Sample] Tomorrow is today, Red printed scarf",
    "id":1,
    "standard_url": "url", 
    "zoom_url":"",
    ...
}

答案 3 :(得分:-1)

杰克逊提供了@JsonUnwrapped注释。

见以下链接:

http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/annotate/JsonUnwrapped.html

相关问题