通用json解析,根据json层级关系直接使用: 基节点.子节点.孙节点.x.x

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
简介: 根据json层级关系直接使用: 基节点.子节点.孙节点.x.x

package com.matol.utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.codehaus.jackson.map.ObjectMapper;

/** 
 *  通用多层json递归解析。在没有Object对象,或是极度复杂的多级嵌套json,情况下以类的方式,直接获取结果。
 *  支持String、Map、ArrayList、ArrayMap四种返回对象的数据获取
 *  使用方式:根据json层级关系直接使用: 基节点.子节点.孙节点
 *  @author ww
 */
public class JsonUtil {

	private static String jsonStr = "{\"api\":\"2.1\",\"message\":[\"产品\",\"tokken\"],\"request\":{\"ptype\":\"JK\",\"tokken\":\"A#daDSFkiwi239sdls#dsd\"},\"response\":{\"status\":{\"statusCode\":\"500\",\"statusMessage\":[\"产品类型错误\",\"tokken失效\"]},\"page\":{\"pageSize\":\"100\",\"pageIndex\":\"1\"},\"data\":{\"ptitle\":\"all product lists\",\"sDate\":\"2014-12-01\",\"eDate\":\"2016-12-01\",\"productList\":[{\"pid\":\"RA001\",\"pname\":\"产品1\"},{\"pid\":\"RA002\",\"pname\":\"产品2\"}]}},\"args\":[{\"tit\":\"RA001\",\"val\":\"产品1\"},{\"tit\":\"RA002\",\"val\":\"产品2\"}]}";
	private static ObjectMapper mapper = new ObjectMapper();
	
	public static void main(String[] args) throws Exception {
		//测试通过json获取Object对象
		//Object obj = getObjectByJson(jsonStr,"response.data.ptitle",TypeEnum.string); //层级递归String
		//System.out.println("API:"+obj.toString());
		
		//Object obj = getObjectByJson(jsonStr,"response.page",TypeEnum.map);  //层级递归Map
		//System.out.println("API:"+obj.toString()+((Map)obj).get("pageSize"));
		
	}
	
	/** 复杂嵌套Map转Json  */
	public static String getJsonByObject(Object obj){
		String str = "";
		try {
			str = mapper.writeValueAsString(obj);
		} catch (Exception e) {
			System.out.println("###[Error] getObjectToJson() "+e.getMessage());
		}
		return str;
	}
	/** 复杂嵌套Json层级展示  */
	public static Object viewJsonTree(Object m){
		if(m == null){ System.out.println("over...");return false;}

		try {
			Map mp = null;
			List ls = null;
			if(m instanceof Map || m instanceof LinkedHashMap){
				mp = (LinkedHashMap)m;
				for(Iterator ite = mp.entrySet().iterator(); ite.hasNext();){  
					Map.Entry e = (Map.Entry) ite.next();  

					if(e.getValue() instanceof String){
						System.out.println("[String]"+e.getKey()+" | " + e.getValue());
					}else if(e.getValue() instanceof LinkedHashMap){
						System.out.println("{Map}"+ e.getKey()+" | "+e.getValue());
						viewJsonTree((LinkedHashMap)e.getValue());
					}else if(e.getValue() instanceof ArrayList){
						System.out.println("[Array]"+ e.getKey()+" | "+e.getValue());
						viewJsonTree((ArrayList)e.getValue());
					}
				}  	
			}
			if(m instanceof List || m instanceof ArrayList){
				ls = (ArrayList)m;
				for(int i=0;i<ls.size();i++){
					if(ls.get(i) instanceof LinkedHashMap){
						viewJsonTree((LinkedHashMap)ls.get(i));
					}else if(ls.get(i) instanceof ArrayList){
						viewJsonTree((ArrayList)ls.get(i));
					}	
				}
			}	
			System.out.println();
		} catch (Exception e) {
			System.out.println("###[Error] viewJsonTree() "+e.getMessage());
		}
		return null;
	}	
	
	
	private int i = 0;
	/** 复杂嵌套Json获取Object数据  */
	public Object getObjectByJson(String jsonStr,String argsPath,TypeEnum argsType){
		if(argsPath == null || argsPath.equals("") || argsType == null){return null;}
		
		Object obj = null;
		try {
			Map maps = mapper.readValue(jsonStr, Map.class);
			//多层获取
			if(argsPath.indexOf(".") >= 0){
				//类型自适应
				obj = getObject(maps,argsPath,argsType);
			}else{ //第一层获取
				if(argsType == TypeEnum.string){
					obj = maps.get(argsPath).toString();
				}else if(argsType == TypeEnum.map){
					obj = (Map)maps.get(argsPath);
				}else if(argsType == TypeEnum.arrayList){
					obj = (List)maps.get(argsPath);
				}else if(argsType == TypeEnum.arrayMap){
					obj = (List<Map>)maps.get(argsPath);
				}
			}
		} catch (Exception e) {
			System.out.println("###[Error] getObjectByJson() "+e.getMessage());
		}
		i = 0;
		return obj;
	}
	//递归获取object
	private Object getObject(Object m,String key,TypeEnum type){
		if(m == null){return null;}
		Object o = null;
		Map mp = null;
		List ls = null;
		try {
			//对象层级递归遍历解析
			if(m instanceof Map || m instanceof LinkedHashMap){
				mp = (LinkedHashMap)m;
				for(Iterator ite = mp.entrySet().iterator(); ite.hasNext();){  
					Map.Entry e = (Map.Entry) ite.next();  
					
					if(i<key.split("\\.").length && e.getKey().equals(key.split("\\.")[i])){
						i++;
						if(e.getValue() instanceof String){
							if(i== key.split("\\.").length){
								o = e.getValue();
								return o;
							}
						}else if(e.getValue() instanceof LinkedHashMap){
							if(i== key.split("\\.").length){
								if(type == TypeEnum.map){
									o = (LinkedHashMap)e.getValue();
									return o;
								}
							}else{
								o = getObject((LinkedHashMap)e.getValue(),key,type);
							}
							return o;
						}else if(e.getValue() instanceof ArrayList){
							if(i== key.split("\\.").length){
								if(type == TypeEnum.arrayList){
									o = (ArrayList)e.getValue();
									return o;
								}
								if(type == TypeEnum.arrayMap){
									o = (ArrayList<Map>)e.getValue();
									return o;
								}
							}else{
								o = getObject((ArrayList)e.getValue(),key,type);
							}
							return o;
						}
					}
				}  	
			}
			//数组层级递归遍历解析
			if(m instanceof List || m instanceof ArrayList){
				ls = (ArrayList)m;
				for(int i=0;i<ls.size();i++){
					if(ls.get(i) instanceof LinkedHashMap){
						if(i== key.split("\\.").length){
							if(type == TypeEnum.map){
								o = (LinkedHashMap)ls.get(i);
								return o;
							}
						}else{
							o = getObject((LinkedHashMap)ls.get(i),key,type);
						}
						return o;
					}else if(ls.get(i) instanceof ArrayList){
						if(i== key.split("\\.").length){
							if(type == TypeEnum.arrayList){
								o = (ArrayList)ls.get(i);
								return o;
							}
							if(type == TypeEnum.arrayMap){
								o = (ArrayList<Map>)ls.get(i);
								return o;
							}
						}else{
							o = getObject((ArrayList)ls.get(i),key,type);
						}
						return o;
					}	
				}
			}	
		} catch (Exception e) {
			System.out.println("###[Error] getObject() "+e.getMessage());
		}
		
		return o;
	}
	
	
	/*
	 * Json数据解析返回数据类型枚举
	 */
	public enum TypeEnum{
		/** 单纯的键值对,通过key获取valus */
        string,
        /** 通过key获取到Map对象 */
        map,
        /** 通过key获取到ArrayList数组 */
        arrayList,
        /** 通过key获取到ArrayMap数组对象 */
        arrayMap;
    }

}

相关文章
|
24天前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
254 0
|
24天前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
78 2
|
24天前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
42 0
|
29天前
|
JSON 数据格式
人脸检测解析json的工具类face_test
人脸检测解析json的工具类face_test
13 0
|
1月前
|
JSON JavaScript 前端开发
如何在Python中解析JSON响应?
【2月更文挑战第26天】【2月更文挑战第92篇】如何在Python中解析JSON响应?
|
1月前
|
JSON JavaScript 前端开发
Qt 5.14.2 深度解析:打造高效JSON处理利器
Qt 5.14.2 深度解析:打造高效JSON处理利器

推荐镜像

更多