2

I have one string from which I want to create one jsonobject

import org.json.JSONObject;

JSONObject json=new JSONObject("{success=false, errorMessage=Application with appId : [randomAppId] not registered, errorCode=102}");
System.out.println("JSON:"+json.toString());

BUT right now I'm getting exception due to characters like :,[,] which is inside the string.

Stacktrace:

Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at character 53
    at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
    at org.json.JSONObject.<init>(JSONObject.java:222)
    at org.json.JSONObject.<init>(JSONObject.java:402)

1 Answer 1

5

The problem is, that your string is not valid JSON. Try:

JSONObject json=new JSONObject("{\"success\":false, \"errorMessage\":\"Application with appId : [randomAppId] not registered\", \"errorCode\":102}");
Sign up to request clarification or add additional context in comments.

2 Comments

@NitinBansal This definitely works. Maybe your problem is different.
@Henry you might be right as I tested it on Android only where it didn't work

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.