一个强大的json解析工具类

该工具类利用递归原理,能够将任意结构的json字符串进行解析。当然,如果需要解析为对应的实体对象时,就不能用了

package com.wot.cloudsensing.carrotfarm.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 将json字符串转换为对应的map等java对象 调用示例: CommonJSONParser commonJSONParser = new
 * CommonJSONParser(); Map<String, Object> result =
 * commonJSONParser.parse(jsonDataStr);
 * 
 */
public class JSonParserUtil {
    public Map<String, Object> parse(String jsonStr) {

        Map<String, Object> result = null;

        if (null != jsonStr) {
            try {

                JSONObject jsonObject = new JSONObject(jsonStr);
                result = parseJSONObject(jsonObject);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } // if (null != jsonStr)

        return result;
    }

    private Map<String, Object> parseJSONObject(JSONObject jsonObject)
            throws JSONException {
        Map<String, Object> valueObject = null;
        if (null != jsonObject) {
            valueObject = new HashMap<String, Object>();

            Iterator<String> keyIter = jsonObject.keys();
            while (keyIter.hasNext()) {
                String keyStr = keyIter.next();
                Object itemObject = jsonObject.opt(keyStr);
                if (null != itemObject) {
                    valueObject.put(keyStr, parseValue(itemObject));
                } // if (null != itemValueStr)

            } // while (keyIter.hasNext())
        } // if (null != valueStr)

        return valueObject;
    }

    private Object parseValue(Object inputObject) throws JSONException {

        Object outputObject = null;
        if (null != inputObject) {

            if (inputObject instanceof JSONArray) {
                outputObject = parseJSONArray((JSONArray) inputObject);
            } else if (inputObject instanceof JSONObject) {
                outputObject = parseJSONObject((JSONObject) inputObject);
            } else if (inputObject instanceof String
                    || inputObject instanceof Boolean
                    || inputObject instanceof Integer) {
                outputObject = inputObject;
            }

        }

        return outputObject;
    }

    private List<Object> parseJSONArray(JSONArray jsonArray)
            throws JSONException {

        List<Object> valueList = null;

        if (null != jsonArray) {
            valueList = new ArrayList<Object>();

            for (int i = 0; i < jsonArray.length(); i++) {
                Object itemObject = jsonArray.get(i);
                if (null != itemObject) {
                    valueList.add(parseValue(itemObject));
                }
            } // for (int i = 0; i < jsonArray.length(); i++)
        } // if (null != valueStr)

        return valueList;
    }
}

 

posted @ 2013-07-23 14:58  bobo的学习笔记  阅读(676)  评论(0编辑  收藏  举报