簡體   English   中英

使用傑克遜排除基於特定值的json屬性

[英]Exclude json property based a specific value using jackson

我有一個看起來像這樣的json文件

{
  "items" : [ {
    "Name" : "India",
    "FullName" : "India",
    "Region" : "Asia"
  }, {
    "Name" : "USA",
    "FullName" : "United States of America",
    "Region" : "Americas"
  }, {
    "Name" : "UK",
    "FullName" : "United Kingdon",
    "DisplayLabel" : "Europe"
  } ]
}

Pojo類看起來像這樣

Countries.java

package model;

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.xml.bind.annotation.XmlElement;

public class Countries {

    @XmlElement( required = true )
    @JsonProperty( "Name" )
    private String name;

    @XmlElement( required = true )
    @JsonProperty( "FullName" )
    private String fullName;

    @XmlElement( required = true )
    @JsonProperty( "Region" )
    private String region;

    public Countries(@JsonProperty( "Name" ) String name, 
                 @JsonProperty( "FullName" ) String fullName,
                 @JsonProperty( "Region" ) String region) {
        this.name = name;
        this.fullName = fullName;
        this.region = region;
    }


    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getFullName() {
        return fullName;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getRegion() {
        return region;
    }
}

Items.java

package model;

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

import java.util.List;

import javax.xml.bind.annotation.XmlElement;

@JsonIgnoreProperties( ignoreUnknown = true )
public class Items {
    public Items() {
        super();
    }

    @XmlElement( required = true )
    @JsonProperty( "items" )
    protected List<Countries> view;


    public void setView(List<Countries> view) {
        this.view = view;
    }

    public List<Countries> getView() {
        return view;
    }

}

我正在使用傑克遜API。 使用ObjectMapper序列化json。 防爆。

Items views = mapper.readValue(jsonString, Items.class);

我不能排除那些具有Region =“ Europe”的條目。 如何實現這一目標。 提前致謝。

Jackson應該專注於序列化和反序列化,而不是在該領域之外執行應用程序邏輯。 您最好反序列化內容,然后遍歷對象以應用您的條件。

List<Country> nonEuropean = views.getView().stream()
        .filter(c -> !c.getRegion().equals("Europe"))
        .collect(Collectors.toList());

從體系結構上講,將Jackson的范圍限制在將JSON轉換為Java和反之亦然的范圍內,使您可以在整個項目中使用相同的Jackson配置。 在當前使用中,您想省略歐洲國家/地區,但是在應用程序的另一個區域中,您可能希望包括這些實體。

我可以帶的最好的是一個自定義的反序列化程序,它過濾掉區域(顯然可以出現在"Region""DisplayLabel" ),但是結果集合將包含空條目,需要刪除后處理:

這是完整的自定義反序列化器

package model;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

public class CountriesDeserializer extends JsonDeserializer<Countries> {
    @Override
    public Countries deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException
    {
        String region = null;
        JsonNode countryNode = p.getCodec().readTree(p);
        JsonNode regionNode = countryNode.get("Region");
        if (regionNode == null) regionNode = countryNode.get("DisplayLabel");
        if (regionNode != null) region = regionNode.asText();
        if ("Europe".equals(region) == false) {
            return new Countries(countryNode.get("Name").asText(), 
                    countryNode.get("FullName").asText(), region);
        }
        return null;
    }   
}

需要在Items類的collection屬性上指定它:

@XmlElement( required = true )
@JsonProperty( "items" )
@JsonDeserialize(contentUsing = CountriesDeserializer.class)
protected List<Countries> view;

調用ObjectMapper的結果是一個具有一個空條目的集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM