使用org.json解决部分日期问题

因为需要对客户端提交至服务端的日期进行处理,对org.json(version 2)以及json2.js进行了些修改.
json2.js
Date.prototype.toJSON = function(key){
      return '@'+this.getUTCFullYear() + '-' +
      f(this.getUTCMonth() + 1) +
      '-' +
      f(this.getUTCDate()) +
      ' ' +
      f(this.getUTCHours()) +
      ':' +
      f(this.getUTCMinutes()) +
      ':' +
      f(this.getUTCSeconds()) +
      '@';
    };

JSONTokener.java
/**
     * Get the next value. The value can be a Boolean, Double, Integer,
     * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
     * @throws JSONException If syntax error.
     *
     * @return An object.
     * @throws  
     */
    public Object nextValue() throws JSONException {
        char c = nextClean();
        String s;

        switch (c) {
            case '"':
           case '\'':
            	String tmp = nextString(c);
            	if(tmp.startsWith("@") && tmp.endsWith("@")){
            		try {
						return formatDate(tmp.substring(1,tmp.length()-1));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						return tmp;
					}
            	}
                return tmp;[/color][/b]
            case '{':
                back();
                return new JSONObject(this);
            case '[':
            case '(':
                back();
                return new JSONArray(this);
        }
.................
       

就可得到日期对象了.
下面通过getMapByJsonData(String json)将json转化为map
public static Map getMapByJsonData(String json) throws JSONException {
		JSONObject jsonObject = new JSONObject(json);
		Map map = jsonObject.getMyHashMap();
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			Object key = (Object) iterator.next();
			Object value = map.get(key);
			if (value instanceof JSONObject) {
				map.put(key, converMap((JSONObject) value));
			} else if (value instanceof JSONArray) {
				map.put(key, converArrayList((JSONArray) value));
			}
		}
		return map;

	}

	private static Map converMap(JSONObject jsonObject) {
		Map map = jsonObject.getMyHashMap();
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			Object key = (Object) iterator.next();
			Object value = map.get(key);
			if (value instanceof JSONObject) {
				map.put(key, converMap((JSONObject) value));
			} else if (value instanceof JSONArray) {
				map.put(key, converArrayList((JSONArray) value));
			}
		}
		return map;
	}

	private static List converArrayList(JSONArray jsonArray) {
		ArrayList list = jsonArray.getMyArrayList();
		if (list == null) {
			return null;
		}
		for (int i = 0; i < list.size(); i++) {
			Object value = list.get(i);
			if (value instanceof JSONObject) {
				list.remove(i);
				value = converMap((JSONObject) value);
				list.add(i, value);
			} else if (value instanceof JSONArray) {
				list.remove(i);
				value = converArrayList((JSONArray) value);
				list.add(i, value);

			}
		}
		return list;
	}


还有点缺陷,就是服务端返回的日期类型,无法通过eval方法转换为日期,不知有哪位高人能指点下.

你可能感兴趣的:(C++,c,json,C#,F#)