My code:
try {
JSONArray array = new JSONArray(response.body());
List<JSONArray> arr = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
arr.add(array.getJSONArray(i));
}
Log.d("arr",arr.toString());
}catch (JSONException e) {
throw new RuntimeException(e);
}
This is my arr
:
[["bdbd","hdhs","hdhd","1234567890","[[shhs, sbbs, married], [sjsn, snsn, unmarried]]"]]
I want to convert the last element inside my arr
"[[shhs, sbbs, married], [sjsn, snsn, unmarried]]"
to a multidimensional ArrayList (ArrayList<ArrayList<String>>
). How do I do this?
2
Answers
After finding no other solution, I just iterated over the whole string and appended each element to the arraylist like this:
There is a one liner solution. Don’t use json.org library use Jackson Json library. In this case all you veed to do is
Also, there is a JsonUtil class that is a thin wrapper over ObjectMapper that allows you to do the same without instantiating ObjectMapper:
JsonUtils comes with MgntUtils OpenSource library written and maintained by me. You can get it as a Maven artifact or on Github (including javadoc and source code). Here is Javadoc for JsonUtils class