使用Jackson库,我正在尝试将数据编码为JSON格式,但出现了异常。
我尝试过字符串数据和字节数组数据:
我尝试过字符串数据和字节数组数据:
String representation of same data is here:
Bytes converted to String-------->>
{
"appname": "aaa",
"deviceType": "diehdcj",
"reportedDate": "2015-05-03T15:38:45+00:00",
"sessionId": "5366372183482-6736-23562378",
"deviceId": "2151272389",
"commandName" : "wqgduwusdue",
"protocolVersion" : "0.1",
"protocolName" : "whjs_ashk_ask",
"data" : "false"
}
Java
16:50:46.065 [] [] AAATSHConnector [http-apr-10.40.120.85-80-exec-3] ERROR - JSON解析异常:无法将VALUE_STRING解码为base64(MIME-NO-LINEFEEDS):在base64内容中存在非法字符“"”(0x22代码)
这是进行解析的代码:
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveDevicePassword(InputStream request, @Context HttpServletRequest servletRequest) throws BadRequestException, ValidationException, UnknownServerException
{
ObjectMapper objectMapper = new ObjectMapper();
DemoRequest req = null;
DemoRequest res = null;
byte[] data = null;
data= IOUtils.toByteArray(request);
DemoRequest = objectMapper.readValue(data, DemoRequest.class); //It's where the Exception occurs
Java
//Snippet of POJO
@XmlRootElement(name = "demoRequest")
@JsonInclude(Include.NON_EMPTY)
public class DemoRequest {
private String commandName;
private String sessionId;
private byte[] data;
//getters & setters
}
令人惊奇的是,当我尝试将相同的字符串或字节数组进行转换时,只需在实际内容中做出一点改变,如将 "data" : "true",它就可以正常工作。
有人能帮忙解决吗?
找到了解决方案,但不知道它具体是做什么的: 如果在 false 前面放一个转义字符,如 "data" : "\false", 它就可以正常工作。
这是什么原因呢?