Attempting to create a JSONArray object with nested arrays and json strings, specifically looking at the "res" field.
[{
"time": 123813213,
"value": [{
"name": "task",
"res": "{\"taskName\" : \"NAME\", \"taskValue\" : 3}"
}]
}]
An issue arises when returning the JSON as a String:
String jsonStr = "[{ \"time\": 123813213, \"value\": [{ \"name\": \"task\", \"res\": \"{\"taskName\", \"taskValue\"}\" }] }]";
JSONArray jsonArr = new JSONArray(jsonStr);
The problem is solved by properly escaping characters in the stored JSON string:
String jsonStr = "[{ \"time\": 123813213, \"value\": [{ \"name\": \"task\", \"res\": \"{\\\"taskName\\\", \\\"taskValue\"}\\\" }] }]";
JSONArray jsonArr = new JSONArray(jsonStr);
In the first case, remember to add extra backslashes. If the JSON string is received from an external source with only one backslash, it may cause issues.
An error is thrown in the first example:
org.json.JSONException: Expected a ',' or '}' at 61 [character 62 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
at org.json.JSONObject.<init>(JSONObject.java:223)
at org.json.JSONTokener.nextValue(JSONTokener.java:362)
(...)
The second example parses correctly :
[{"time":123813213,"value":[{"res":"{\"taskName\", \"taskValue\"}","name":"task"}]}]
The goal is to make the first example return this result without errors.