1

I didn't expect this is that complex. But I can't figure out how I can convert my arraylist of custom class to a JSONArray. I understand there is no direct method to do that. So I made arraylist of JSONObjects from my arraylist. Now in order to save this into a JSON file, how can I convert the arraylist of JSONObjects to JSONArray?

2 Answers 2

2

Like this:

JSONArray toReturn = new JSONArray();
for(JSONObject object : yourJSONArrayList){
    toReturn.put(object);
}

toReturn will then be a JSONArray that has all the JSONObjects from your arraylist of JSONObjects in it.

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

1 Comment

Laughing at myself... As a old .NET developer, I searched add or insert. Thanks!
1

Give Gson Library a try, all that custom json conversion code goes away.

1 Comment

I thought about it before but since our app has relatively large file size, I didn't want to add another 200K. But thanks anyway.

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.