循环取多层嵌套的JsonArray中的值,最后一层可以是JsonArray也可以是jsonObject
程序员文章站
2022-06-05 13:46:27
...
/**
* @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;
}