日期:2014-05-16  浏览次数:20431 次

ext json请求java后台返回集合数据
JAVA后台类文件:JsonUtil.java

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 JsonUtil { 
    public static void main(String[] args) { 
    JsonUtil j = new JsonUtil(); 
        //j.bean2json();
    j.list2json();
    //j.map2json();
    //j.json2bean();
    } 
 
    public JSONArray arr2json() { 
        boolean[] boolArray = new boolean[] { true, false, true }; 
        JSONArray jsonArray = JSONArray.fromObject(boolArray); 
        System.out.println(jsonArray);
       
        // prints [true,false,true]
        return jsonArray;
    } 
 
    public JSONObject list2json() { 
        List list = new ArrayList(); 
        //list.add("first"); 
        //list.add("second"); 
       
        Map map1 = new HashMap();
        map1.put("name", "json"); 
        map1.put("sex", "男");

 
        Map map2 = new HashMap();
        map2.put("name", "json222"); 
        map2.put("sex", "男222");
 
        list.add(map1);
        list.add(map2);
       
    Map map = new HashMap();
map.put("root", list);
JSONObject json = JSONObject.fromObject(map);

System.out.println(json);
        //JSONArray jsonArray = JSONArray.fromObject(list); 
        // prints ["first","second"]
       
        return json;
    } 
 
    public void createJson() { 
        JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']"); 
        System.out.println(jsonArray); 
        // prints ["json","is","easy"] 
    } 
 
    public JSONObject map2json() { 
        Map map = new HashMap();
        map.put("name", "json"); 
        map.put("sex", "男");
        map.put("bool", Boolean.TRUE); 
        map.put("int", new Integer(1)); 
        map.put("arr", new String[] { "a", "b" }); 
        map.put("func", "function(i){ return this.arr[i]; }"); 
 
        JSONObject json = JSONObject.fromO