我正在尝试使用fasterxml jackson反序列化json,其中一个字段是字符串,但我需要将其读取为字节数组,请参阅以下Bean和主类:
public class Serialization implements Serializable{
private static final long serialVersionUID = 5894318390213780082L;
private String name = null;
@JsonDeserialize(using = StringtoByteArray.class)
private byte[] pass = null;
//getter setter
}
public class StringtoByteArray extends JsonDeserializer<byte []> {
@Override
public byte[] deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
return (jsonParser.getBinaryValue());
}
}
public class App
{
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
Serialization sr = new Serialization();
ObjectMapper mapper = new ObjectMapper();
sr = mapper.readValue(new File("D:\\check.json"), Serialization.class);
System.out.println("sr values ::" +sr.toString());
mapper.writeValue(new File("c:\\user.json"), sr);
}
}
我的Json
{
"name": "AD",
"pass": "pp"
}
反序列化时出现异常
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: 将VALUE_STRING解码为base64(MIME-NO-LINEFEEDS)失败:在base64内容中存在非法字符“"”(代码0x22)
请提供建议
getBytes(StandardCharsets.UTF_8)
。 - Joop Eggen