JSON 的封装和解析
下面是一个演示JSON数据从 对象转换成JSON,最后翻译成对象 的一个演示实例代码。
//MapList转换成json, 然后再 解释读出来里面的元素 @Test public String testJSON(){ Map map1 = new HashMap(); map1.put( "name", "json" ); map1.put( "bool", Boolean.TRUE ); map1.put( "int", new Integer(1) ); map1.put( "float",0.5f); Map map2 = new HashMap(); map2.put( "name", "json2" ); map2.put( "bool", Boolean.TRUE ); map2.put( "int", new Integer(1) ); map2.put( "float",0.6f); List list = new ArrayList(); list.add(map1); list.add(map2); Map map_main = new HashMap(); map_main.put( "error_no", "error_no" ); map_main.put( "error_info","error_info" ); map_main.put("result", list); JSONObject jsonObject = JSONObject.fromObject(map_main); System.out.println( jsonObject.toString() ); List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("result")); for(Object object : arrayList){ JSONObject jsonObject_temp = JSONObject.fromObject( object ); System.out.println(jsonObject_temp.get("name")); } return jsonObject.toString(); }
JAVA端的对HTTP的JSON访问参照 http://www.cnblogs.com/mywebname/articles/2130636.html