映射到Java对象时如何忽略几个JSON元素

时间:2019-02-06 20:46:25

标签: java json spring spring-boot

我的Spring Boot服务处理这样的JSON

"Books":[
  {
   "Title": "Title1",
   "Author": "Author1"
  },
  {
   "Title": "Title2",
   "Author": "Author2"
  },
  {
   "Title": "IGNORE",
   "Author": "IGNORE"
  }
]

我有一本Book.java

public class Book{
  @JsonProperty("Title")
  private String title;
  @JsonProperty("Author")
  private String author;

  public setters & Getters
  ....
}

这些书被映射到MyFavBooks.java

public class MyFavBooks{
  @JsonProperty("Books")
  private Book[] books;
  ....
}

我正在尝试不映射以下元素(基于标题)。有什么办法吗?

{
   "Title": "IGNORE",
   "Author": "IGNORE"
} 

2 个答案:

答案 0 :(得分:1)

您可以尝试使用@JsonSerialize自定义序列化程序。像这样:

public class Book{
  @JsonProperty("Title")
  @JsonSerialize(using = IgnoreSerializer.class)
  private String title;

  @JsonProperty("Author")
  @JsonSerialize(using = IgnoreSerializer.class)
  private String author;

  public setters & Getters
  ....
}
public class IgnoreSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String s, 
                          JsonGenerator jsonGenerator, 
                          SerializerProvider serializerProvider) 
                          throws IOException, JsonProcessingException {
        if(!s.equals("IGNORE")) {
            jsonGenerator.writeObject(s);
        }
    }
}

或者,如果您需要根据“标题”值跳过整个项目,请在Book类上使用相同的@JsonSerialize定义自定义序列化程序。

@JsonSerialize(using = IgnoreByTitleSerializer.class)
public class Book{ ... }

class IgnoreByTitleSerializer extends StdSerializer<Book> {
...
@Override
    public void serialize(
      Book book, JsonGenerator jgen, SerializerProvider provider) 
      throws IOException, JsonProcessingException {
        if(!book.getTitle().equals("IGNORE") {
            ...
        }
    }
}

例如,请参见https://www.baeldung.com/jackson-custom-serialization

upd
正如德米特里·博格达诺维奇(Dmitry Bogdanovich)所说的那样,问题涉及反序列化,因此我相信您可以使用@JsonDeserialize进行类似的处理。

答案 1 :(得分:0)

如果使用gson,瞬变将起作用。如果使用杰克逊,请使用@JsonIgnore。

示例:

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

public class Book {
    private String title;
    private String author;

    @JsonIgnore
    private transient String isbn;


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    private static void printGson(Book book) {
        System.out.println("Gson: " + new Gson().toJson(book));
    }

    private static void printJackson(Book book) {
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            System.out.print("Jackson: ");
            objectMapper.writeValue(System.out, book);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Book book = new Book();
        book.setAuthor("Me");
        book.setTitle("Cool");
        book.setIsbn("123");
        printGson(book);
        printJackson(book);
    }
}

输出:

Gson: {"title":"Cool","author":"Me"}
Jackson: {"title":"Cool","author":"Me"}