简体   繁体   English

无法从具有双引号的 Json 创建 Java 对象

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

I have same query.我有同样的疑问。 My JSON is as below.我的 JSON 如下。

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. key = password 和 value 是"Password"123"它包含 " (双引号).. 我无法从这个 json 创建一个 java 对象,因为它是无效的。

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.如果这是一个真实的场景,那么我想建议更改源以确保它提供有效的 JSON。

There are countless possibilities to check if your JSON is valid (JSON linting), here's one .有无数种可能来检查您的 JSON 是否有效(JSON linting),这里有一个.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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