gpt4 book ai didi

java - JSON解析异常: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '"' (code 0x22) in base64 content

转载 作者:行者123 更新时间:2023-11-30 10:39:54 32 4
gpt4 key购买 nike

使用 Jackson,我正在尝试用 JSON 编码数据,但它给出了异常。

我尝试了 String 数据和 byte[] 数据:

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 [] [] ERROR AAATSHConnector [http-apr-10.40.120.85-80-exec-3] - JSON Parsing Exception: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '"' (code 0x22) in base64 content

这是执行解析的代码:

Java

@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

}

令人惊讶的是,当我尝试转换相同的 String 或 byte[] 并在实际内容 "data": "true"中稍作更改时,它起作用了。

谁能帮忙

找到了解决方案,但不知道它到底在做什么:如果我在 false 前面放置一个转义字符,比如 "data": "\false",它工作正常。

对此有何解释?

最佳答案

JSON Parsing Exception: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '"' (code 0x22) in base64 content

当属性类型为byte[]且json内容用String表示时出现上述异常。 Jackson 认为在这种情况下,json 数据表示为 base64 编码,并尝试将其解码为 byte[]。如果数据字段中的字符串不是正确编码的 base64 字符串,jackson 会引发上述异常。

关于java - JSON解析异常: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '"' (code 0x22) in base64 content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39116449/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com