主要还是MAP转换成JSONArray
package com.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TestClass {
public static void main(String[] args) {
List<Map<String,String>> lists = new ArrayList<Map<String,String>>();
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 5; i++) {
map.put(i+"", i+"--");
}
lists.add(map);
JSONArray jsonArray = toJsonArray(lists);
String fileName = "test"+6+".html";
File file = new File("E:/"+fileName);
try {
file.createNewFile();
BufferedWriter output = new BufferedWriter(new FileWriter(file));
output.write(jsonArray.toString());
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static JSONArray toJsonArray( List<Map<String, String>> list ){
JSONArray jsonarray=new JSONArray();
for (int i=0;i<list.size();i++){
Map<String,String> listmap=null;
listmap=list.get(i);
JSONObject data = JSONObject.fromObject(listmap);
jsonarray.add(data);
}
return jsonarray;
}
}
[{"0":"0--","1":"1--","2":"2--","3":"3--","4":"4--"}]