使用json-lib进行json操作

使用json-lib来进行json操作需要先添加以下几个jar包:

ezmorph-1.0.2.jar

commons-beanutils-1.8.3.jar

commons-collections-3.2.1.jar

commons-lang-2.6.jar

commons-logging-1.1.1.jar

json-lib-2.4-jdk15.jar

其中版本可以按照需求进行调整,并不是固定的。

/**
	 * 获得所有列表按照“id,name,ltime,size”的形式返回
	 * @return
	 */
	public String getAllList() {
		MapWrapper mw = MapWrapper.make();
		mw.put("token", token).put("dir_id", 0);// dir_id 目录id, 如果是根目录值为: 0
		String resp = Http.post(VDiskConstant.SERVER + "?m=dir&a=getlist", mw.map(), Encoding.UTF8, Encoding.UTF8);
		if (resp == null) return null;
		JSONObject jsonObject = JSONObject.fromObject(resp);
		JSONObject jsonObjectData = JSONObject.fromObject(jsonObject.get("data"));
		JSONArray jsonArrayList = JSONArray.fromObject(jsonObjectData.get("list"));
		JSONObject jsonObjectContent = null;
		StringBuffer sBuffer = new StringBuffer();
		for(int i=0;i<jsonArrayList.size();i++){
			jsonObjectContent = JSONObject.fromObject(jsonArrayList.get(i));
			sBuffer.append(jsonObjectContent.get("id")).append(",")
					.append(jsonObjectContent.get("name")).append(",")
					.append(jsonObjectContent.get("ltime")).append(",")
					.append(jsonObjectContent.get("size")).append("|");
		}
		return sBuffer.toString();
	}

上面是一段使用json-lib来进行解析json的例子,

首先将json串通过JSONObject来进行解析,将json串转为JSONObject对象,如下:

JSONObject jsonObject = JSONObject.fromObject(resp);
下面这段代码是将JSONObject对象中data部分的内容也转化为JSONObject对象,如下:

JSONObject jsonObjectData = JSONObject.fromObject(jsonObject.get("data"));
下面这段代码是将JSONObjectData对象中list部分的内容转化为JSONArray对象。此外为什么用JSONArray来进行解析呢?原因是list部分由多段组成,也就是类似于多个JSONObject,所以必须先用JSONArray来解析,如下:

JSONArray jsonArrayList = JSONArray.fromObject(jsonObjectData.get("list"));
最后就遍历jsonArrayList对象,再将每一项都转代为JSONObject对象。

下面给出具体的JSON串:

{
    "err_code": 0,
    "err_msg": "success",
    "data": {
        "list": [
            {
                "id": "805093852",
                "name": "DownDlg.cpp",
                "dir_id": "0",
                "ctime": "1375085574",
                "ltime": "1375085574",
                "size": "615 Bytes",
                "type": "text/x-c",
                "md5": "25e4946e3daef011cd5fdb38a90c7bee",
                "sha1": "5fb18ae6d3c766ffa92a057bb8d52939e7df9f08",
                "hid": "0",
                "share": -1,
                "byte": "615",
                "length": "615",
                "url": "http://openapi.vdisk.me/open_file/31317c383035303933383532/9e6e2e80a215dd4eadfc25aa4c1a5ff6/DownDlg.cpp"
            },
            {
                "id": "779676735",
                "name": "3.txt",
                "dir_id": "0",
                "ctime": "1374136457",
                "ltime": "1375078198",
                "size": "10 Bytes",
                "type": "text/plain",
                "md5": "49c854414f25bcd8e9ceb39889fde70b",
                "sha1": "a7881ab7a1d309e3b06f1dc6827dc7961fc7c819",
                "hid": "0",
                "share": -1,
                "byte": "10",
                "length": "10",
                "url": "http://openapi.vdisk.me/open_file/31317c373739363736373335/9e6e2e80a215dd4eadfc25aa4c1a5ff6/3.txt"
            },
            {
                "id": "779441458",
                "name": "5fae76d865b0624b5f155bfc.pdf",
                "dir_id": "0",
                "ctime": "1374130532",
                "ltime": "1374130532",
                "size": "590.56 KB",
                "type": "application/pdf",
                "md5": "6a273a69ba18faea4ff4e5059b3601b2",
                "sha1": "3179fb1da51bd5ada3ad3684261fbca13a69ce9d",
                "hid": "0",
                "share": -1,
                "byte": "604730",
                "length": "604730",
                "url": "http://openapi.vdisk.me/open_file/31317c373739343431343538/9e6e2e80a215dd4eadfc25aa4c1a5ff6/%E5%BE%AE%E7%9B%98%E6%96%B0%E6%89%8B%E5%BC%95%E5%AF%BC.pdf"
            },
            {
                "id": "195335913",
                "name": "624b673a59074efd",
                "pid": "0",
                "ctime": "1374130531",
                "ltime": "1374130531",
                "file_num": "0",
                "dir_num": "0"
            },
            {
                "id": "195335912",
                "name": "65876863",
                "pid": "0",
                "ctime": "1374130531",
                "ltime": "1374130531",
                "file_num": "0",
                "dir_num": "0"
            },
            {
                "id": "195335911",
                "name": "56fe7247",
                "pid": "0",
                "ctime": "1374130531",
                "ltime": "1374130531",
                "file_num": "0",
                "dir_num": "0"
            }
        ],
        "pageinfo": {
            "page": 1,
            "pageSize": 1024,
            "rstotal": 6,
            "pageTotal": 1
        }
    },
    "dologid": "921040154",
    "dologdir": []
}

关于json-lib的其他api可以参考其它网上例子,为例子只为介绍json-lib的使用,以及理解JSONObject和JSONArray对象的使用场景。


转载于:http://www.16boke.com

你可能感兴趣的:(json,json-lib)