簡體   English   中英

如何轉換List <Entity> 到一個字符串?

[英]How to convert List<Entity> to a string?

這是代碼:我有一個名為ClassA的實體,它由以下屬性組成

  @JsonProperty("rowDeleted")
    private Boolean rowDeleted;
    @JsonProperty("start")
    private List<Start> start = null;
    @JsonProperty("end")
    private List<End> end = null;
    @JsonProperty("rows")
    private List<Row> rows = null;

And Row是另一個由屬性組成的實體:

 @JsonProperty("cells")
    private List<Cell> cells = null;
    @JsonProperty("clusteringKey")
    private String clusteringKey;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

而Cell是另一個實體:

 @JsonProperty("deleted")
    private Boolean deleted;
    @JsonProperty("name")
    private String name;
    @JsonProperty("value")
    private String value;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

我得到一個ClassA的對象,並希望將其轉換為另一個實體,其中ClassB包含字段:

private String end;
    private String key;
    private String keyspacename;
    private String partitiondeleted;
    private String rowdeleted;
    private String rows;
    private String start;
    private String tablename;
    private String triggerdate;
    private String triggertime;

所以基本上我想將ClassA的List行轉換為ClassB的String行。

任何人都可以建議一種方法來做到這一點。 提前致謝

假設你有一個A類的清單。

 List<A> list= . . . ;
 List<B> newList=list
  .stream()
  .map(obj-> new B()
     .setKey(obj.getKey())
     .setKeyspacename(//set which field of class A will be saved)
     .setPartitiondeleted()
     // set more fields
    )
  .collect(Collecters.toList());

然后序列化此newlist使用jakson成字符串。

我想要一個可以代表json格式的字符串,因此根據我的要求修改了我的toString(),它解決了我的目的。

暫無
暫無

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

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