1. 程式人生 > >迴圈取多層巢狀的JsonArray中的值,最後一層可以是JsonArray也可以是jsonObject

迴圈取多層巢狀的JsonArray中的值,最後一層可以是JsonArray也可以是jsonObject

/**
 * @param jsonStr json字串
 * @param key     具體取值上一級對應的所有的 Key
 * @param value   具體取值對應的所有的key
 * @param type    type 是最後一個key對應的是array還是jsonObject 如果true則對應的是jsonArray
 * @Author:
 * @Description:迴圈取JsonArray中具體的值
 * @Date: 2018/8/3 11:09
 * @Method jsonPattern2
 * @Return java.util.List<java.lang.String>
 * 例:具體取值為clientName,instanceName
 * key傳入的是'subClientProperties:subClientEntity'
 * value傳入的是'clientName,instanceName'
 * type : false
 * {
 * "subClientProperties": [{
 * "proxyClient": {},
 * "subClientEntity": {
 * "clientName": "server-e37203ba",
 * "instanceName": "DefaultInstanceName",
 * "backupsetId": 3
 * }
 * }, {
 * "proxyClient": {},
 * "subClientEntity": {
 * "clientName": "server-e37203ba",
 * "instanceName": "SERVER-E37203BA\\COMMVAULT",
 * "backupsetId": 9
 * }
 * }]
 * }
 */
public static List<String> jsonPattern2(String jsonStr, String key, String value,
    boolean type)
{
    String[] pats = key.split(":");//得到key對應的陣列
    // String[] keys = pats[0].split(",");
    int jsonObjectPos = 0;
    int jsonArrayPos = 0;
    JSONArray ja = new JSONArray();
    a:
    for (int z = 0; z < pats.length; z++)
    {
        Object j = new JSONTokener(JSONObject.fromObject(jsonStr).get(pats[z]).toString()).nextValue();
        if (j instanceof JSONArray)
        {
            ja = getJsonArray(jsonStr, pats[z]); //得到對應的jsonArray
            jsonObjectPos = z;
            jsonArrayPos++;
            break a;
        }
        else
        {
            jsonStr = j.toString();
        }
    }
    //得到jsonArray的大小,後面遍歷使用
    int len = ja.size();
    // 返回list
    List<String> list = new ArrayList<>();
    //得到取哪些key值得value,最低一層
    String[] pat_detail = value.split(",");
    //取Jsonarray中對應的值
    if (type)
    {
        //對jsonArray遍歷
        for (int i = 0; i < len; i++)
        {
            //拼接得到的值
            StringBuffer buffer = new StringBuffer();
            buffer.append("{");
            //對每個JsonArray中想取的值進行遍歷取值
            for (int y = 0; y < pat_detail.length; y++)
            {
                buffer.append("\"" + pat_detail[y] + "\":\"" +
                    (JSONObject.fromObject(ja.get(i)).getString(pat_detail[y]) + "\","));
            }
            String s = buffer.toString();
            StringBuffer re2 = new StringBuffer();
            s = s.substring(0, s.length() - 1);
            re2.append(s);
            re2.append("}");
            list.add(re2.toString());
        }
    }
    //取JsonObject中對應的值
    else
    {
        Object json = "";
        //遍歷對應的JsonArray
        for (int i = 0; i < len; i++)
        {
            //判斷 取到JsonArray後 key是否是最後一個,如果不是則繼續取出最後一個key所對應的值
            if (jsonObjectPos != pats.length - 1)
            {
                for (int yy = jsonArrayPos; yy <= pats.length - jsonArrayPos; yy++)
                {
                    json = new JSONTokener(ja.getJSONObject(i).get(pats[yy]).toString()).nextValue();
                }
            }
            else
            {
                json = new JSONTokener(ja.getJSONObject(i).get(pats[jsonObjectPos]).toString()).nextValue();
            }
            //  Object json = new JSONTokener(ja.getJSONObject(i).get(pats[jsonObjectPos]).toString()).nextValue();
            //如果是jsonObject 則取出對應的引數
            if (json instanceof JSONObject)
            {
                StringBuffer result = new StringBuffer();
                result.append("{");
                for (int y = 0; y < pat_detail.length; y++)
                {
                    if (!(JSONObject.fromObject(json).get(pat_detail[y]) instanceof JSONObject))
                    {
                        result.append(
                            "\"" + pat_detail[y] + "\":\"" + ((JSONObject)json).get(pat_detail[y]) + "\",");
                    }
                    else
                    {
                        json = JSONObject.fromObject(json).get(pat_detail[y]);
                    }
                }
                String s = result.toString();
                StringBuffer re2 = new StringBuffer();
                s = s.substring(0, s.length() - 1);
                re2.append(s);
                re2.append("}");
                list.add(re2.toString());
            }
            else if (json instanceof JSONArray)
            {
                //System.out.println("JSONArray");
            }
        }

    }
    return list;
}