Convert ArrayList<Map<String, String>> to jsonArray

i need to convert my ArrayList<Map<String, String>> object to the JSONArray

ArrayList<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
//I tried this

JSONArray jsonArray = new JSONArray(dataList);  

// I am importing (java-json.jar) for converting ArrayList to JSONArray
// But i can't put this array in to JqGrid in my jsp page it's does not give me any output


You can try Google's Gson library for easy conversion of Java objects to Json and vice versa

Try below code

Gson gson = new Gson();
JsonArray jsonArray = gson.toJsonTree(dataList).getAsJsonArray();

EDIT : Convert jsonArray to string

String jsonStr = gson.toJson(jsonArray); //This will convert JsonArray to Java string

Need Your Help

Counting number of elements to be sorted in insertion sort algorithm

python algorithm sorting python-3.x

I think the title is self-explanatory. I am trying to write a program that could count how many swaps are needed to sort a list using insertion sort. This is what I have done:

Playing youtube video in Android app

android android-intent youtube android-mediaplayer

In my Android app I'd like the user to tap an image once, have a youtube video play automatically and when the video is done the user is immediately returned to the app. What's the best way to do t...