【问题标题】:Deserializing a JSON array using Jackson使用 Jackson 反序列化 JSON 数组
【发布时间】:2013-04-23 05:55:06
【问题描述】:

我收到来自 3rd 方服务提供商的 JSON 响应,其中包含一组对象。 当我尝试使用 Jackson api 反序列化 JSON 时。我收到以下异常

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of JacksonFields out of START_ARRAY token
 at [Source: java.io.BufferedReader@1015a9e; line: 5, column: 26]

我的 JSON 响应是

{
    "flags" : 1074200577,
    "results" : {
        "id1" : 0,
        "id2" : 0,
        "fields" : [
            {
                "id1" : 19202,
                "id2" : 19202,
                "count" : 0,
                "format" : 8,
                "type" : "name",
                "flags" : 0,
                "group" : 1074,
                "value" : "1074"
            },
            {
                "id1" : 19218,
                "id2" : 19218,
                "count" : 0,
                "format" : 8,
                "type" : "name",
                "flags" : 0,
                "group" : 1075,
                "value" : "1075"
            }
        ]
    }
}

我的 POJO 类看起来像这样

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
class JacksonFields {
    int id1;
    int id2;
    int count;
    int format;
    String type;
    int flags;
    int group;
    String value;

    public JacksonFields(){

    }

    @JsonCreator
    public JacksonFields(@JsonProperty("id1") int id1,
            @JsonProperty("id2") int id2,
            @JsonProperty("count") int count,
            @JsonProperty("format") int format,
            @JsonProperty("type") String type,
            @JsonProperty("flags") int flags,
            @JsonProperty("group") int group,
            @JsonProperty("value") String value){
        this.id1 = id1;
        this.id2 = id2;
        this.count = count;
        this.format = format;
        this.type = type;
        this.flags = flags;
        this.group = group;
        this.value = value;
    }

    public void putId1(int id){
        this.id1=id;
    }

    public void putId2(int id){
        this.id2=id;
    }

    public void putCount(int count){
        this.count=count;
    }

    public void putFormat(int format){
        this.format=format;
    }

    public void putType(String type){
        this.type=type;
    }

    public void putFlag(int flag){
        this.flags=flag;
    }

    public void putGroup(int group){
        this.group=group;
    }

    public void putValue(String val){
        this.value=val;
    }
}


class JacksonResults {
    int id1;
    int id2;
    JacksonFields fields;

    @JsonCreator
    public JacksonResults(@JsonProperty("id1") int id1,
            @JsonProperty("id2") int id2,
            @JsonProperty("fields") JacksonFields fields){
        this.id1 = id1;
        this.id2 = id2;
        this.fields = fields;
    }

    public JacksonResults(){

    }

    public void putId1(@JsonProperty("id1") int id){
        this.id1 = id;
    }

    public void putId2(@JsonProperty("id2") int id){
        this.id2 = id;
    }

    public void putFields(@JsonProperty("fields") JacksonFields fie){
        this.fields = fie;
    }
}


public class JacksonJsonObj{
    Long flags;
    JacksonResults res;

    @JsonCreator
    public JacksonJsonObj(@JsonProperty("flags") long flags, 
            @JsonProperty("results") JacksonResults res){
        this.flags = flags;
        this.res = res;
    }

    public JacksonJsonObj(){

    }

    public void putFlags(@JsonProperty("flags") long flag){
        this.flags = flag;
    }

    public void putResults(@JsonProperty("results") JacksonResults res){
        this.res=res;
    }
}

我正在尝试使用以下代码反序列化 JSON

ObjectMapper objmapper = new ObjectMapper();
objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JacksonJsonObj jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonJsonObj.class);

如果我尝试这样做

JacksonJsonObj[] jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonJsonObj[].class);

它在 BEGIN_OBJECT 本身失败。

如何使用数组读取和反序列化 JSON。我应该编写自己的反序列化器吗?

编辑 如果我处理 JSON 字符串而不是流,我能够取回所有 Java 对象。但为了获得更好的表现,我希望杰克逊能够在直播中工作

另一种方式

List<JsonFields> JsonFieldsJackson = new ArrayList<JsonFields>();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode nodes = mapper.readTree(strbuffer.toString());
nodes.elements();
Iterator<JsonNode> iter = nodes.path("results").path("fields").elements();
while(iter.hasNext()){
    JsonNode node = iter.next();
    JsonFields fie = mapper.readValue(node.toString(),JsonFields.class);
    JsonFieldsJackson.add(fie);
}

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

    我正在考虑您已经有 2 个罐子,即 1。杰克逊核心 2.杰克逊映射器

    所以从 JSON 解析到你的 POJO

        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType=mapper.getTypeFactory().constructType(JacksonFields.class);
    
        JacksonFields jksnflds = mapper.readValue(jsonString,javaType);
    

    就是这样!。

    【讨论】:

      【解决方案2】:

      要反序列化 JSON,你应该有 3 个类

      class Field{
      int id1;
      int id2;
      int count;
      int format;
      String type;
      int flags;
      int group;
      String value;
      
      } 
      class Result{
      int id1;
      int id2;
      Field[] fields;
      }
      
      class JacksonFields {
      String flags;
      Result result; 
      
      }
      

      然后你可以写类似的代码

      JacksonFields jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonFields.class);
      

      然后它就会工作。 注意:-我没有为您可以提供的类提供适当的注释。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多