通过Json数据的路径和数据类型获取到json中的值demo
程序开发
2023-09-21 12:44:47
通过Json数据的路径和数据类型获取到json中的值demo
package com.example.demo.testjson2;import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;import java.util.Iterator;
import java.util.List;
import java.util.Map;/*** @author LiaoLiChao* @date 2023/1/9 17:07*/
@Slf4j
public class JsonToolsFastJson {// 用于记录递归的次数private int i = 0;/*** 复杂嵌套JSON获取Object数据*/public Object getObjectByJson(String jsonStr, String argsPath, com.example.demo.testjson.JsonToolsFastJson.TypeEnum argsType) {if (StringUtils.isBlank(argsPath) || argsType == null) {log.info("必填参数argsPath或argsType不能为空");return null;}Object obj = null;try {Map maps = JSONObject.parseObject(jsonStr);//多层获取if (argsPath.contains(".")) {obj = getObject(maps, argsPath, argsType);} else {//直接获取if (argsType == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.STRING) {obj = JSONObject.parseObject(jsonStr).get(argsPath);} else if (argsType == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.MAP) {obj = (Map) JSONObject.parseObject(jsonStr).get(argsPath);} else if (argsType == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.LIST) {obj = (List) JSONObject.parseObject(jsonStr).get(argsPath);}}} catch (Exception e) {log.error(e.getMessage());}return obj;}// 递归获取objectprivate Object getObject(Object m, String key, com.example.demo.testjson.JsonToolsFastJson.TypeEnum type) {if (m == null) {System.out.println("over...");return null;}// 返回的对象Object o = null;Map mp = null;List ls = null;try {// 第二层只会出现{}或[]//{}对象层级递归遍历解析if (m instanceof Map) {mp = (Map) m;for (Iterator ite = mp.entrySet().iterator(); ite.hasNext(); ) {Map.Entry e = (Map.Entry) ite.next();// e : json中的key-value// key : 传入的pathif (i < key.split("\.").length && e.getKey().equals(key.split("\.")[i])) {i++;if (e.getValue() instanceof String) {//递归最后一次if (i == key.split("\.").length) {o = e.getValue();i = 0;return o;}} else if (e.getValue() instanceof Map) {//递归最后一次if (i == key.split("\.").length) {if (type == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.MAP) {o = (Map) e.getValue();i = 0;return o;}} else {o = getObject((Map) e.getValue(), key, type);}return o;} else if (e.getValue() instanceof List) {//递归最后一次if (i == key.split("\.").length) {if (type == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.LIST) {o = (List) e.getValue();i = 0;return o;}} else {o = getObject((List) e.getValue(), key, type);}return o;}}}}//[]数组层级递归遍历解析// 获取[]数据时,只能直接获取所有[]数据if (m instanceof List) {ls = (List) m;for (int i = 0; i < ls.size(); i++) {if (ls.get(i) instanceof Map) {//递归最后一次if (i == key.split("\.").length) {if (type == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.MAP) {o = (Map) ls.get(i);return o;}} else {o = getObject((Map) ls.get(i), key, type);}return o;} else if (ls.get(i) instanceof List) {//递归最后一次if (i == key.split("\.").length) {if (type == com.example.demo.testjson.JsonToolsFastJson.TypeEnum.LIST) {o = (List) ls.get(i);return o;}} else {o = getObject((List) ls.get(i), key, type);}return o;}}}} catch (Exception e) {log.error(e.getMessage());}return o;}/*** JSON数据解析返回数据类型枚举,我将value归为3类*/public enum TypeEnum {STRING,MAP,LIST;}private static String jsonStr = "{"_id":"5973782bdb9a930533b05cb2","isActive":true,"balance":"$1,446.35","age":32,"eyeColor":"green","name":"Logan Keller","gender":"male","company":"ARTIQ","email":"logankeller@artiq.com","phone":"+1 (952) 533-2258","friends":[{"id":0,"name":"Colon Salazar"},{"id":1,"name":"French Mcneil"},{"id":2,"name":"Carol Martin"}],"mobian":[{"id":0,"name":"Colon Salazar","arr":[{"id":0,"name":"Colon Salazar"}]}],"favoriteFruit":"banana"}";public static void main(String[] args) {//测试通过json获取Object对象com.example.demo.testjson.JsonToolsFastJson jsonTools = new com.example.demo.testjson.JsonToolsFastJson();Object str1 = jsonTools.getObjectByJson(jsonStr, "name", com.example.demo.testjson.JsonToolsFastJson.TypeEnum.STRING);log.info("str1:" + str1);Object str3 = jsonTools.getObjectByJson(jsonStr, "friends", com.example.demo.testjson.JsonToolsFastJson.TypeEnum.LIST);log.info("str3:" + str3);Object str4 = jsonTools.getObjectByJson(jsonStr, "mobian.arr", com.example.demo.testjson.JsonToolsFastJson.TypeEnum.LIST);log.info("str4:" + str4);}
}
测试结果:
标签:
上一篇:
IntelliJ IDEA安装及相关设置
下一篇:
相关文章
-
无相关信息