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

JSON 工具类,操作JSON类型数据
public static JSONObject parseJson(String json){
		return JSON.parseObject(json);
	}
	
	public static Map<String,Object> toMapFromJsonString(String json){
		return JsonUtils.toMap(JsonUtils.parseJson(json));
	}
	
	/**
	 * 将JSONObjec对象转换成Map-List集合
	 * @param json
	 * @return
	 */
	public static Map<String, Object> toMap(JSONObject json){
	    Map<String, Object> map = new HashMap<String, Object>();
	    Set<Entry<String, Object>> entrySet = json.entrySet();
	    for (Iterator<Entry<String, Object>> iter = entrySet.iterator(); iter.hasNext(); ){
	    	Entry<String, Object> entry = iter.next();
	    	String key = entry.getKey();
	    	Object value = entry.getValue();//value
	        if(value instanceof JSONArray)
	            map.put((String) key, toList((JSONArray) value));
	        else if(value instanceof JSONObject)
	            map.put((String) key, toMap((JSONObject) value));
	        else
	            map.put((String) key, value);
	    }
	    return map;
	}

	/**
	 * 将JSONArray对象转换成List集合
	 * @param json
	 * @return
	 */
	public static List<Object> toList(JSONArray json){
	    List<Object> list = new ArrayList<Object>();
	    for (int i=0; i<json.size(); i++){
	    	Object value = json.get(i);
	    	if(value instanceof JSONArray){
	            list.add(toList((JSONArray) value));
	    	}
	        else if(value instanceof JSONObject){
	            list.add(toMap((JSONObject) value));
	        }
	        else{
	            list.add(value);
	        }
	    }
	    return list;
	}

?