Home » Java » not able to create Java Object from Json having double quote in value-Exceptionshub

not able to create Java Object from Json having double quote in value-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have same query. My JSON is as below.

String json="{ "credentials": { "password": "Password"123", "emailAddress": "[email protected]" }, "clientTimeMs": 1582006455421, "callerRole": 0 }"

key = password and value is “Password”123” it contains ” (double quote).. I am not able to create a java object from this json as it is invalidated.

Gson gson = new Gson(); 
gson.fromJson(json, PasswordResetDetails.java);

Above code snippet is not Working.

How to&Answers:

If you are doing this for learning / testing purpose all you need to do is escaping the double quote using :

String json="{ "credentials": { "password": "Password\"123", "emailAddress": "[email protected]" }, "clientTimeMs": 1582006455421, "callerRole": 0 }"

If this is a real scenario then I would like to suggest to change the source in order to make sure it provides valid JSON.

There are countless possibilities to check if your JSON is valid (JSON linting), here’s one.