用jackson反序列化

ep6jt1vc  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(406)

我需要一种反序列化此对象的方法:

{
  "rows": [
    {
      "id": 0,
      "name": "qwe"
    }
  ],
  "total": 0
}

不使用wapper对象的行[](我不需要“total”):

public class ReviewsWrapper {
    private Row[] rows;
    @JsonIgnore
    private Integer total;
}

直接反序列化到行[]?如果没有“total”对象,我将使用以下方法反序列化:

public static <T> T fromJsonWithRootName(InputStream is, Class<T> type, String rootName) {
    try {
        return objectMapper.reader(type).withRootName(rootName).readValue(is);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

将“rows”作为根名称传递,并将获取行[]作为输出。有没有办法避免对行[]使用wrapperobject?这是一个android项目,我使用jackson注解定义实体。

yrdbyhpb

yrdbyhpb1#

json数据模型类应该是这样的

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({
"rows",
"total"
})
public class ReviewsWrapper {

@JsonProperty("rows")
private List<Row> rows = new ArrayList<Row>();
@JsonProperty("total")
private long total;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**

* 
* @return
* The rows
* /

@JsonProperty("rows")
public List<Row> getRows() {
return rows;
}

/**

* 
* @param rows
* The rows
* /

@JsonProperty("rows")
public void setRows(List<Row> rows) {
this.rows = rows;
}

public ReviewsWrapper withRows(List<Row> rows) {
this.rows = rows;
return this;
}

/**

* 
* @return
* The total
* /

@JsonProperty("total")
public long getTotal() {
return total;
}

/**

* 
* @param total
* The total
* /

@JsonProperty("total")
public void setTotal(long total) {
this.total = total;
}

public ReviewsWrapper withTotal(long total) {
this.total = total;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public ReviewsWrapper withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}

}
-----------------------------------com.example.Row.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({
"id",
"name"
})
public class Row {

@JsonProperty("id")
private long id;
@JsonProperty("name")
private String name;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**

* 
* @return
* The id
* /

@JsonProperty("id")
public long getId() {
return id;
}

/**

* 
* @param id
* The id
* /

@JsonProperty("id")
public void setId(long id) {
this.id = id;
}

public Row withId(long id) {
this.id = id;
return this;
}

/**

* 
* @return
* The name
* /

@JsonProperty("name")
public String getName() {
return name;
}

/**

* 
* @param name
* The name
* /

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

public Row withName(String name) {
this.name = name;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public Row withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
}

然后在java类中使用jackson反序列化使用下面的代码

ObjectMapper mapper = new ObjectMapper();
JsonFactory jf = new JsonFactory();

JsonParser jp = jf.createJsonParser("your json data as a String");

ReviewsWrapper reviewWrapper = mapper.readValue(jp,ReviewsWrapper.class);

在此之后,您可以从“reviewswrapper.class”获得所有响应
下面是一个使用jsonnode的示例。这就是你想要的吗?
下面是一个使用节点的示例。

public class App {
    public static class Foo {
        public int foo;
    }
    public static void main(String[] args) {

        String json = "{\"someArray\":[{\"foo\":5},{\"foo\":6},{\"foo\":7}]}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(json);
        node = node.get("someArray");
        TypeReference<List<Foo>> typeRef = new TypeReference<List<Foo>>(){};
        List<Foo> list = mapper.readValue(node.traverse(), typeRef);
        for (Foo f : list) {
            System.out.println(f.foo);
        } }}

相关问题