1. /** 

  2.     * 将json格式的字符串解析成Map对象 

  3.  

  4.     * json格式:{"name":"admin","retries":"3fff","testname" 

  5.     * :"ddd","testretries":"fffffffff"} 

  6.     */  

  7.    private static HashMap toHashMap(Object object)  

  8.    {  

  9.        HashMap data = new HashMap();  

  10.        // 将json字符串转换成jsonObject  

  11.        JSONObject jsonObject = JSONObject.fromObject(object);  

  12.        Iterator it = jsonObject.keys();  

  13.        // 遍历jsonObject数据,添加到Map对象  

  14.        while (it.hasNext())  

  15.        {  

  16.            String key = String.valueOf(it.next());  

  17.            String value = (String) jsonObject.get(key);  

  18.            data.put(key, value);  

  19.        }  

  20.        return data;  

  21.    }  

  22.  把List转换成JSON数据:


  23. List list = new ArrayList();

  24. UserInfo user = new UserInfo(1001, "张三");

  25. list.add(user);

  26. list.add(user);

  27. list.add(user);

  28. JSONArray jsonArray = JSONArray.fromObject(list);

  29. System.out.println(jsonArray);

把Map转换成json, 要使用jsonObject对象:


Map map = new HashMap();

map.put("userId", 1001);

map.put("userName", "张三");

map.put("userSex", "男");

JSONObject jsonObject = JSONObject.fromObject(map);

System.out.println(jsonObject);

http://jingyan.baidu.com/article/1709ad80b780f04634c4f0d9.html

对象转换成JSON:

UserInfo user = new UserInfo(1001,"张三");

JSONArray jsonArray = JSONArray.fromObject(user);  

System.out.println( jsonArray )

;  

将数组转换为JSON:

String[] arr = {"asd","dfgd","asd","234"};

JSONArray jsonarray = JSONArray.fromObject(arr);

System.out.println(jsonarray);