我如何告诉Json在解析ObjectMapper对象时“不要转义”?换句话说,按原样返回字符串。例如,我的JSON是这样的:
{"field_1":"xyz","field_2":"ABWKJ\/m3ERpLr"}
通过ObjectMapper运行后,field2值是:“ABWKJ/m3ERpLr”,但我想要"ABWKJ\/m3ERpLr“‘,因为我需要解密它&解密失败,因为'\’反斜杠没有了。
我尝试了以下几点:
MyClass jsonMessage = mapper.readValue(input, MyClass);
以及:
MyClass jsonMessage = mapper.readerFor(MyClass).readValue(input.getBytes());
但是两个版本都在某种程度上改变了我的字符串。我想要回“原样”。我应该使用不同的类吗?
发布于 2016-11-18 07:47:52
我知道现在有点晚了,但我也有类似的问题。
我找到的一个解决方案是使用JsonRawValue打印字段的原始值。
public class MyClass{
private String myField1;
private String myField2;
@JsonRawValue
public String getMyField1() {
return myField1;
}
@JsonRawValue
public String getMyField2() {
return myField2;
}
}请注意,由于某些原因,如果您将一个属性设置为JsonRawValue,则还需要为其他属性添加注释。
我不是100%确定这是否是最好的解决方案,但它的工作,让我知道如果你找到了更好的解决方案。
发布于 2018-03-14 21:03:43
@DilTeam,
@JsonRawValue有时似乎并不总是有效的,我建议使用字符串并检查令牌。我有同样的问题,如下所示,它适用于我。
String responseClone = finalResponse; // finalResponse =Json Response string
String pinValue = null;
if(null != responseClone){
responseClone = responseClone.replace("{", "");
responseClone = responseClone.replace("}", "");
responseClone = responseClone.replace("\"", "");
String[] strNodeSplit = responseClone.split(",");
LOG.debug("Splited response");
for (String stringNode : strNodeSplit) {
int j =0 ;
String[] strValueSplit = stringNode.split(":");
for (String strValue : strValueSplit) {
LOG.debug(j +" Value :" +" "+strValue);
if(strValue.equalsIgnoreCase("PIN")){
pinValue = strValueSplit[++j];
LOG.debug("Pin equals value : "+pinValue);
break;
}
j++;
}
}
}https://stackoverflow.com/questions/37422875
复制相似问题