一个通用的json获取value值的api

两个参数:

参数1:json的String

参数2:json树的文法表达式,需要自己约定,这里简单约点*表示数组,默认最后是终结点结束(也可以使用$表示结束的key值)

 

代码:

package com.utils.json;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtils {
	public String[] getvalue(String jsonStr, String xPath) {
		if (jsonStr == null || "".equals(jsonStr) || xPath == null || "".equals(xPath))
			return null;

		if (xPath.startsWith("/"))
			xPath = xPath.substring(1);

		String[] paths = xPath.split("/");
		if (paths.length == 0)
			return null;
		int curIndex = 0;
		JSONObject jsonObject = JSON.parseObject(jsonStr);
		if (jsonObject == null)
			return null;
		List<String> resultList = new ArrayList<>();
		// 遍历json
		traversing(jsonObject, paths, curIndex, resultList);
		// 转成string
		if (resultList.size() == 0)
			return null;
		String[] results = new String[resultList.size()];
		for (int i = 0; i < resultList.size(); i++) {
			results[i] = resultList.get(i);
		}
		return results;
	}

	private void traversing(JSONObject jsonObject, String[] paths, int curIndex, List<String> resultList) {
		String curPath = paths[curIndex];
		if (curIndex == paths.length - 1) {// 是最终节点
			if (curPath.endsWith("*")) {
				// 是数组
				JSONArray jsonArray = jsonObject.getJSONArray(curPath.replace("*", ""));
				for (Object object : jsonArray) {
					resultList.add((String) object);
				}
			} else {
				// 不是数组
				String value = jsonObject.getString(curPath);
				resultList.add(value);
			}
		} else {
			// 不是最终节点
			if (curPath.endsWith("*")) {
				// 是数组
				JSONArray jsonArray = jsonObject.getJSONArray(curPath.replace("*", ""));
				for (int i = 0; i < jsonArray.size(); i++) {
					traversing(jsonArray.getJSONObject(i), paths, curIndex + 1, resultList);
				}
			} else {
				// 不是数组
				traversing(jsonObject.getJSONObject(curPath), paths, curIndex + 1, resultList);
			}
		}
	}

	public static void main(String[] args) {
		String json = "{'province':{'name':'guangdong','city':{'company':['huawei','tencent'],'name':'shenzheng','person':[{'name':'1'},{'name':'2'},{'name':'3'}]}}}";
		JsonUtils jsonUtils = new JsonUtils();
		String[] getvalue = jsonUtils.getvalue(json, "/province/city/person*/name");
		for (String value : getvalue) {
			System.out.println(value);
		}
	}
}

测试json:

{
    "province": {
        "name": "guangdong",
        "city": {
            "company": ["huawei", "tencent"],
            "name": "shenzheng",
            "person": [{
                "name": "1"
            }, {
                "name": "2"
            }, {
                "name": "3"
            }]
        }
    }
}

进行了写简单测试,可能还会有bug 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月玲珑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值