I have a simple json str like below
{
"foo":"\uv"
}
I want to use gson to parse this str to jsonElement. eg.
String input = "{\"foo\":\"\\uv\"}";
JsonElement element = JsonParser.parseString(input);
But gson throw the com.google.gson.JsonSyntaxException, java.lang.NumberFormatException: \uv
It seems like when JsonReader meet the '\', it will automaticly treat it as a escape character.So, What can I do to make gson treat it as plain text instead of escape character?
What you want to do should not be possible. Forcing gson to accept \
as plain text would be forcing it to not follow json conventions.
Also your json is not valid, correct one would be:
{
"foo":"\\uv"
}
Check gson adding backslash in string, it has good explanation. Using your code as example:
public class Temp {
public static void main(String[] args) {
String input = "{\"foo\":\"\\\\uv\"}";
JsonElement element = JsonParser.parseString(input);
System.out.println("foo value - " + element.getAsJsonObject().getAsJsonPrimitive("foo").getAsString());
}
}
This prints - foo value - \uv
, which should be exactly the value your consumers need.