Jackson Parser无法读取String中的反斜杠引号 [英] Jackson Parser can't read backslash quotation marks in String

查看:1012
本文介绍了Jackson Parser无法读取String中的反斜杠引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获得了这样的JSON:

I've got JSON like this from the server:

{
  "id":"1",
  "value":13,
  "text":"{\"Pid\":\"2\",\"value\":42}"
}

我使用jackson库通过以下代码将此JSON字符串反序列化为java对象:(例子如下)

I am using jackson library to deserialize this JSON string into java object with this code: (example below)

ObjectMapper mapper = new ObjectMapper();
MapObj obj = mapper.readValue(JSONfromServerInString, MapObj.class);

其中Map Object如下所示:

where Map Object looks like that:

public class MapObj {
        @JsonProperty("id")
        private Integer id;
        @JsonProperty("value")
        private Integer value;
        @JsonProperty("text")
        private String text;

        public Integer getId() {return id;}
        public void setId(Integer id) {this.id = id;}
        public Integer getValue() {return value;}
        public void setValue(Integer value) {this.value = value;}
        public String getText() {return text;}
        public void setText(String text) {this.text = text;}
    }

但是当我尝试用引号前的反斜杠反序列化这个String时。杰克逊解串器似乎在他找到字符串的第一个结尾时结束。他忽略了反斜杠。因此该示例将输出:

But when I try to deserialize this String with Backslashes before quotation marks. Jackson deserializer seems to end when he finds the first end of the string. He ignore that backslash. So the example will output this:


org.codehaus.jackson.JsonParseException:意外字符('P'(代码80)):was期望逗号分隔OBJECT条目
at [来源:java.io.StringReader@2f7c7260; line:1,column:33]

org.codehaus.jackson.JsonParseException: Unexpected character ('P' (code 80)): was expecting comma to separate OBJECT entries at [Source: java.io.StringReader@2f7c7260; line: 1, column: 33]

(('P'(代码80))代表原文中的P字符JSON字符串\Pid \)

(The ('P' (code 80)) stands for the P character in the original JSON string \"Pid\")

推荐答案

你确定它不起作用吗?此测试工作正常:

Are you sure it does not work? This test works fine:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        String json = "{\n" +
                "  \"id\":\"1\",\n" +
                "  \"value\":13,\n" +
                "  \"text\":\"{\\\"Pid\\\":\\\"2\\\",\\\"value\\\":42}\"\n" +
                "}";

        MapObj mapObj = objectMapper.readValue(json, MapObj.class);
        System.out.println("text = " + mapObj.getText());
    }

    private static class MapObj {
        @JsonProperty("id")
        private Integer id;
        @JsonProperty("value")
        private Integer value;
        @JsonProperty("text")
        private String text;

        public Integer getId() {return id;}
        public void setId(Integer id) {this.id = id;}
        public Integer getValue() {return value;}
        public void setValue(Integer value) {this.value = value;}
        public String getText() {return text;}
        public void setText(String text) {this.text = text;}
    }
}

打印:

text = {"Pid":"2","value":42}

使用Jackson 2.6.4

Using Jackson 2.6.4

这篇关于Jackson Parser无法读取String中的反斜杠引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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