1

Below is my jsonString:

jsonString= {"date_of_birth":"17-2-1989 ","email_id":"[email protected]","fullname":"a","hp_id":"Wellcare","password":"a","phone_no":"12345","ss_no":"12345","username":"a"}

I wants to add "User" object to it

so I did this:

JSONObject jsonObj=new JSONObject();
jsonObj.put("User", jsonString);

but I am getting this:

{"User":"{\"date_of_birth\":\"17-2-2015 \",\"email_id\":\"[email protected]\",\"fullname\":\"a\",\"healthplan_provider_id\":\"Wellcare\",\"password\":\"a\",\"phone_no\":\"12345\",\"social_security_no\":\"12345\",\"username\":\"a\"}"}

An extra character '\' is added. So I want to remove it. Please help removing it.

0

1 Answer 1

5

Don't add your JSON as a String. Add it as a JSONObject.

JSONObject jsonObj=new JSONObject();
jsonObj.put("User", new JSONObject(jsonString));

When you add it as a String, you lose the ability to query User properties via JSON methods. This is because the JSON parser now treats the complete object as a literal string and hence escapes all the quotes as well.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding a little more explanation: Notice the quote immediately after the first colon. That's how you can tell that the entire object is being added as a single string. The '\'s are there to allow that string to contain embedded quotes.
(plus 1): If you put it as a String, JSONObject will escape it so another parser can read this "String".
Hi ravi, I am adding jsonobjects inside a list with a loop, will this work for me? because when i am using this its taking only last object value of jsonobject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.