转换ArrayList <map <string,string =“”>&gt;到jsonArray </map <string,>

时间:2014-03-14 07:05:09

标签: java json arraylist

我需要将ArrayList<Map<String, String>>对象转换为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

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Google的Gson库轻松将Java对象转换为Json,反之亦然

尝试以下代码

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

编辑:将jsonArray转换为字符串

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