无法从具有值双引号的Json创建Java对象 [英] not able to create Java Object from Json having double quote in value

查看:128
本文介绍了无法从具有值双引号的Json创建Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相同的查询.我的JSON如下所示.

I have same query. My JSON is as below.

String json="{ "credentials": { "password": "Password"123", "emailAddress": "skylineadmin@gmail.com" }, "clientTimeMs": 1582006455421, "callerRole": 0 }"

键=密码,值是密码" 123" ,它包含(双引号)..由于此json无效,我无法从其创建Java对象.

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);

以上代码段无效.

推荐答案

如果您出于学习/测试目的而这样做,那么您要做的就是使用:

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 }"

如果这是一个真实的场景,那么我建议更改源,以确保它提供有效的JSON.

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是否有效(JSON标记)的方法,这里是一个.

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

这篇关于无法从具有值双引号的Json创建Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆