JSON递归解析(二)

根据表达式递归解析json获取某个属性的值,如果属性为数组则对数组元素进行拼接:
json示例:

{
    "root":[
        {
            "elementA":{
                "test":null
            },
            "elementBs":[
                {
                    "elementNo":"test01",
                    "elementName":"testName1",
                    "elementEvent":[
                        "event11","event12"
                    ]
                },
                {
                    "elementNo":"test02",
                    "elementName":"testName2",
                    "elementEvent":[
                        "event21","event22"
                    ]
                }
            ]
        },
        {
            "elementA":{
                "test":456
            },
            "elementBs":null
        }
    ]
}

表达式示例:
root.elementBs.elementNo
结果:
test01(#)test02
json解析使用GSON,以下为代码:


    /**
     * 递归获得json元素的值,如果元素为数组,对数组元素进行拼接,分隔符为`(#)`
     * @param jsonElement
     * @param params
     * @return
     */
    public static String recursiveGetElementString(JsonElement jsonElement,String[] params){
        StringBuilder stringBuilder = recursiveGetElementString(jsonElement, params, 0);
        String result=stringBuilder.toString();

        //去除结尾分隔符
        if(StringUtils.isNotBlank(result)){
            result = result.replaceAll("(\\(#\\))+$", "");
        }
        return result;
    }
	
	 public static StringBuilder recursiveGetElementString(JsonElement jsonElement,String[] params,int index){
        if (jsonElement==null || jsonElement.isJsonNull()){
            return null;
        }

        final JsonObject jsonObject = jsonElement.getAsJsonObject();
        StringBuilder stringBuilder=new StringBuilder();

        if (index< params.length-1){
            JsonElement element = jsonObject.get(params[index++]);
            if (element==null || element.isJsonNull()){
                return null;
            }
            //如果元素是数组,对数组元素进行拼接
            if (element.isJsonArray()){
                JsonArray jsonArray = element.getAsJsonArray();
                for (JsonElement arrayElement : jsonArray) {
                    StringBuilder string = recursiveGetElementString(arrayElement, params, index);
                    stringBuilder.append(string==null?"":string);
                }
            }else {
                StringBuilder string = recursiveGetElementString(element, params, index);
                stringBuilder.append(string==null?"":string);
            }
        }else {
            final String jsonValue = getJsonValueWithSplicing(jsonObject.get(params[index]));
            if (StringUtils.isNotBlank(jsonValue)){
                stringBuilder.append(jsonValue).append("(#)");
            }else {
                stringBuilder.append("(#)");
            }
        }
        return stringBuilder;
    }
	
	    /**
     * 获得json元素的值,如果元素为数组对数组元素进行字符串拼接
     * @param jsonElement
     * @return
     */
    public static String getJsonValueWithSplicing(JsonElement jsonElement){
        if(jsonElement==null){
            return null;
        }
        if (jsonElement.isJsonArray()){
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            int size = jsonArray.size();
            if (size ==0){
                return null;
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < size -1; i++) {
                if (jsonArray.get(i)!=null){
                    stringBuilder.append(getJsonValue(jsonArray.get(i))).append("(#)");
                }
            }
            stringBuilder.append(getJsonValue(jsonArray.get(size-1)));
            return stringBuilder.toString();
        }else {
            return getJsonValue(jsonElement);
        }
    }

测试:

    public static void main(String[] args) {
        String json="{\n" +
                "    \"root\":[\n" +
                "        {\n" +
                "            \"elementA\":{\n" +
                "                \"test\":null\n" +
                "            },\n" +
                "            \"elementBs\":[\n" +
                "                {\n" +
                "                    \"elementNo\":\"test01\",\n" +
                "                    \"elementName\":\"testName1\",\n" +
                "                    \"elementEvent\":[\n" +
                "                        \"event11\",\"event12\"\n" +
                "                    ]\n" +
                "                },\n" +
                "                {\n" +
                "                    \"elementNo\":\"test02\",\n" +
                "                    \"elementName\":\"testName2\",\n" +
                "                    \"elementEvent\":[\n" +
                "                        \"event21\",\"event22\"\n" +
                "                    ]\n" +
                "                }\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"elementA\":{\n" +
                "                \"test\":456\n" +
                "            },\n" +
                "            \"elementBs\":null\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        String params="root.elementBs.elementNo";
        String[] split = params.split("\\.");
        JsonParser jsonParser = new JsonParser();
        final JsonElement jsonElement = jsonParser.parse(json);

        final String s = recursiveGetElementString(jsonElement, split);
        System.out.println(s);
    }
上一篇:Java日期和时间戳互转(看这一篇就够了)


下一篇:StringBuffer和StringBuilder