Reputation: 2661
How do I tell Jackson ObjectMapper to 'NOT escape' while parsing a Json object? In other words, return the String 'as is'. For example, my JSON is like this:
{"field_1":"xyz","field_2":"ABWKJ\/m3ERpLr"}
After running thru ObjectMapper, field2 value is:"ABWKJ/m3ERpLr", but I want "ABWKJ\/m3ERpLr" 'cause I need to decrypt it & decryption is failing because the '\' backslash is gone.
I tried following:
MyClass jsonMessage = mapper.readValue(input, MyClass);
as well as:
MyClass jsonMessage = mapper.readerFor(MyClass).readValue(input.getBytes());
But both version change my String in some way. I want it back 'AS IS'. Should I use a different class?
Upvotes: 2
Views: 10324
Reputation: 59
@DilTeam,
@JsonRawValue seems sometimes does not work all the time, I would recommend to take the string and check the token. I had same issue and used as below it works for me.
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++;
}
}
}
Upvotes: 1
Reputation: 3875
I know it is a bit late but I had a similar problem.
A solution I found is using JsonRawValue to print the fields raw values.
public class MyClass{
private String myField1;
private String myField2;
@JsonRawValue
public String getMyField1() {
return myField1;
}
@JsonRawValue
public String getMyField2() {
return myField2;
}
}
Note that for some reason if you set one attribute as JsonRawValue, you need to add annotations for other attributes as well.
I'm not 100% sure if this is the best solution for the problem but it works, let me know if you find a better solution.
Upvotes: 6