Jackson自定义反序列化器String to List,并删除\\\\ n [英] Jackson custom deserializer String to List, and remove \r\n

查看:206
本文介绍了Jackson自定义反序列化器String to List,并删除\\\\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务获得以下JSON作为响应。我使用Spring RestTemplate来调用服务,该服务也将JSON反序列化为Object。我试图将其反序列化为一个除了其他字段之外还有List的Object。反序列化时出现以下错误:

I am getting the following JSON as response from a service. I am using Spring RestTemplate to call the service which also deserializes the JSON back into an Object. I am trying to deserialize it into an Object that has a List besides other fields. I am getting the following error while deserializing:

Can not instantiate value of type [simple type, class com.org.EmployeeInfo] from String value; no single-String constructor/factory method.

以下是我要反序列化的JSON:

Following is the JSON that i want to deserialize:

{
    "employees": {       
        "employeeInfo": [
            "{\r\n  \"id\": \"123\",\r\n  \"group\": \"MARKETING\",\r\n  \"role\": \"MANAGER\",\r\n}",
            "{\r\n  \"id\": \"256\",\r\n  \"group\": \"IT\",\r\n  \"role\": \"DIRECTOR\",\r\n}",
            "{\r\n  \"id\": \"789\",\r\n  \"group\": \"SALES\",\r\n  \"role\": \"CEO\",\r\n}"
        ]
    },
    "status": "EMPLOYED",
    "somethingElse": {
        "something": []
    }
}

如果我有<$ c $,则默认的反序列化器会失败c>在我尝试将respone映射到的对象中列出< EmployeeInfo> ,但如果我使用 List< String> 字符串[] 。这是因为JSON中的双引号(我说的是{\\\\ n),这使得它被视为一个字符串

The default deserializer fails if i have the List<EmployeeInfo> in the object that i try to map the respone to, but it works if i use List<String> or String[]. This is because of the double quotes in the JSON (I am talking about "{\r\n) which makes it treat as a String

我打算编写一个自定义反序列化器,将其反序列化为一个具有List的对象,并删除响应部分的\\\\ n。我该怎么做?感谢任何回复。

I am planning to write a custom deserializer to deserialize it into an object having List and also remove the \r\n that's part of the response. How can i do that? Appreciate any responses.

以下是我的POJO:

public class Response {
    private Employees employees;
    private String status;
    private SomethingElse somethingElse; 

   // getters, setters
}

public class Employees {
    List<EmployeeInfo> employeeInfo;
    // getters, setters
}

public class EmployeeInfo
{
   private String id, group, role;
   // getters, setters 
}

谢谢

推荐答案

我通过以下方式解决了这个问题(一些逻辑需要是o ptimized)

I worked around this problem in the following way (some of the logic needs to be optimized)

public class EmployeeInfoJsonDeserializer extends JsonDeserializer<List<EmployeeInfo>> {

    @Override
    public List<EmployeeInfo> deserialize(final JsonParser jp, final DeserializationContext ctxt)
                    throws IOException, JsonProcessingException {

        final ObjectMapper mapper = (ObjectMapper) jp.getCodec();

        final JsonNode node = (JsonNode) mapper.readTree(jp);

        // TODO - Write the following logic in a better way
        String toStr = node.toString();
        toStr = StringUtils.replace(toStr, "\"{", "{");
        toStr = StringUtils.replace(toStr, "}\"", "}");
        toStr = StringUtils.remove(toStr, "\\r\\n");
        toStr = StringUtils.remove(toStr, "\\");

        final JsonNode newNode = mapper.readTree(toStr);

        final EmployeeInfo[] empInfo = mapper.convertValue(newNode, EmployeeInfo[].class);

        return Arrays.asList(empInfo);
    }
}

在Pojo:

public class Employees {
       List<EmployeeInfo> employeeInfo;

       public List<EmployeeInfo> getEmployeeInfo() {
            return employeeInfo;
       }

       @JsonDeserialize(using = EmployeeInfoJsonDeserializer .class)
       public void setEmployeeInfo(final List<EmployeeInfo> employeeInfo) {
            this.employeeInfo= employeeInfo;
       }
}

这篇关于Jackson自定义反序列化器String to List,并删除\\\\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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