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

╄→гoц情女王★ 提交于 2020-03-28 06:40:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/60299306/not-able-to-create-java-object-from-json-having-double-quote-in-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!