将对象添加到 JSON 字符串时添加额外的字符“\”

Ron*_*tya 1 android json

下面是我的 jsonString:

jsonString= {"date_of_birth":"17-2-1989 ","email_id":"s@s.a","fullname":"a","hp_id":"Wellcare","password":"a","phone_no":"12345","ss_no":"12345","username":"a"}
Run Code Online (Sandbox Code Playgroud)

我想向其中添加“用户”对象

所以我这样做了:

JSONObject jsonObj=new JSONObject();
jsonObj.put("User", jsonString);
Run Code Online (Sandbox Code Playgroud)

但我得到这个:

{"User":"{\"date_of_birth\":\"17-2-2015 \",\"email_id\":\"s@s.a\",\"fullname\":\"a\",\"healthplan_provider_id\":\"Wellcare\",\"password\":\"a\",\"phone_no\":\"12345\",\"social_security_no\":\"12345\",\"username\":\"a\"}"}
Run Code Online (Sandbox Code Playgroud)

添加了一个额外的字符“\”。所以我想把它去掉。请帮忙删除它。

Rav*_*yal 5

不要将 JSON 添加为String. 将其添加为JSONObject.

JSONObject jsonObj=new JSONObject();
jsonObj.put("User", new JSONObject(jsonString));
Run Code Online (Sandbox Code Playgroud)

当您将其添加为字符串时,您将无法通过 JSON 方法查询用户属性。这是因为 JSON 解析器现在将完整的对象视为文字字符串,因此也会转义所有引号。