"{\"SuccessData\": \"Data fetched successfully\",
\"ErrorData\": \"\",
\"AppData\": \"[{\\\"uniqe_id\\\":{\\\"appId\\\":4,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2223,\\\"appName\\\":ACMP\\\"},{\\\"uniqe_id\\\":{\\\"appId\\\":5,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2225,\\\"appName\\\":ICMP\\\"}]\"
}"
The real problem here is that this input is not valid JSON.
Let's assume that these are the exact characters that you got in your response; i.e. the first character is a double-quote. But a valid JSON object starts with a {
character. Not even whitespace is allowed according to strict reading of the syntax graph at https://json.org.
But what if that is actually a Java String
literal representing the JSON?
In that case, the JSON is valid1. And what is more, your code for the JSON is correct. when I compile and run this, it works ... without throwing an exception.
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
String response = "{\"SuccessData\":\"Data fetched successfully\",\"ErrorData\":\"\",\"AppData\":\"[{\\\"uniqe_id\\\":{\\\"appId\\\":4,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2223,\\\"appName\\\":ACMP\\\"},{\\\"uniqe_id\\\":{\\\"appId\\\":5,\\\"agentId\\\":1,\\\"isActive\\\":1\\\"},\\\"pid\\\":2225,\\\"appName\\\":ICMP\\\"}]\"}";
JSONObject jsonObj = new JSONObject(response);
}
}
Ergo, if you are getting a JSONException
then the input is not a Java String
literal.
1 - I wouldn't say it was correct. The AppData
attribute has a value that is a string not a JSON object. But that string is a JSON serialization. This is technically valid, but it is a poor design choice.