简体   繁体   中英

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

I have same query. My JSON is as below.

String json="{ "credentials": { "password": "Password"123", "emailAddress": "skylineadmin@gmail.com" }, "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.

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": "skylineadmin@gmail.com" }, "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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM