如何将带有数组字段的JSON序列化为带有String字段的对象? [英] How to serialize JSON with array field to object with String field?

查看:329
本文介绍了如何将带有数组字段的JSON序列化为带有String字段的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

{
  "id" : "1",
  "children" : ["2","3"]
}

我有一个Java对象,例如(省略了构造函数,getter和setter):

And I have a Java object like (constructor, getters and setters are omitted):

public class Entity {
    public String id;
    public String children;
}

我希望使用Jackson通过此代码将此JSON反序列化为我的Java对象:

I want this JSON to be deserialized to my Java object by this code using Jackson:

Entity entity = mapper.readValue(json, Entity.class);

但是出现以下错误:

Can not deserialize instance of java.lang.String out of START_ARRAY token

如何在不更改children字段类型的情况下解决该问题?

How can I solve it without changing type of children field?

children字段应具有以下值:["2","3"].

The children field is expected to have the following value: ["2","3"].

推荐答案

创建自定义解串器

创建一个自定义反序列化器以获取原始JSON值.您可以根据需要选择以下实现之一:

Creating a custom deserializer

Create a custom deserializer to get the raw JSON value. You can choose one of the following implementations, according to your needs:

  1. 它将按原样为您提供JSON ,即保留所有空格和制表符:
  1. It will give you the JSON as is, that is, keeping all the spaces and tabs:

public class RawJsonDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt)
           throws IOException, JsonProcessingException {

        long begin = jp.getCurrentLocation().getCharOffset();
        jp.skipChildren();
        long end = jp.getCurrentLocation().getCharOffset();

        String json = jp.getCurrentLocation().getSourceRef().toString();
        return json.substring((int) begin - 1, (int) end);
    }
}

  1. 它将为您提供不带多余空格和制表符的JSON:

public class RawJsonDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt)
           throws IOException {

        JsonNode node = jp.getCodec().readTree(jp);
        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
        return mapper.writeValueAsString(node);
    }
}

注释您的类以使用上面定义的反序列化器

通过引用上面定义的反序列化器的@JsonDeserialize注释children属性来更改Entity类:

Annotate your class to use the deserializer defined above

Change the Entity class by annotating the children attribute with @JsonDeserialize referencing the deserializer defined above:

public class Entity {

    public String id;

    @JsonDeserialize(using = RawJsonDeserializer.class)
    public String children;
}

解析JSON

然后使用ObjectMapper解析JSON,Jackson将使用您的自定义反序列化器:

Parsing the JSON

Then parse the JSON using ObjectMapper and Jackson will use your custom deserializer:

String json = "{\"id\":\"1\",\"children\":[\"2\",\"3\"]}";

ObjectMapper mapper = new ObjectMapper();
Entity entity = mapper.readValue(json, Entity.class);

children属性的值将为["2","3"].

有关更多详细信息,请查看此问题.

For more details, have a look at this question.

这篇关于如何将带有数组字段的JSON序列化为带有String字段的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆