赞
踩
基于接口的质量检核,需要对接口数据进行标准化(类似表结构)。由于接口数据由数据共享平台拦截接口保存数据,故各种接口的返回数据形式各不相同,为了质量检核得以实现,对拦截的接口数据进行相应的实体抽取得到标准化数据
假设有如下数据:
- {
- "code": "200",
- "total": 1,
- "data": [
- {
- "people":{
- "name":"SS",
- "age":10,
- "sex":"F",
- "address":{
- "country":"中国",
- "city":"重庆市",
- "street":"皇室桀骜"
- }
- }
- },{
- "people":{
- "name":"SS",
- "age":10,
- "sex":"F",
- "address":{
- "country":"中国",
- "city":"重庆市",
- "street":"皇室桀骜"
- }
- }
- },{
- "people":{
- "name":"SS",
- "age":10,
- "sex":"F",
- "address":{
- "country":"中国",
- "city":"重庆市",
- "street":"皇室桀骜"
- }
- }
- },{
- "people":{
- "name":"SS",
- "age":10,
- "sex":"F",
- "address":{
- "country":"中国",
- "city":"重庆市",
- "street":"皇室桀骜"
- }
- }
- }
- ]
- }

假设我需要拿到address的相关信息。我的json路径就是data.people.address
但是需要对每个people进行解析取出address,所以为了快速取出数据,根据路径,定制了下面的规则。
同样以取address为目标,规则路径为data[?].people.address ;data为key,[]代表数组,?代表data下的每一个对象都需要进行解析,如果为数字0,1,2则表示取第几个元素。
由于数据层级与数据是对象还是数组的不确定性,所以采用递归的方式进行实现
- package org.zxhy.tool;
-
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * @author Ryan
- * @version 1.0
- * @date 2021/9/9
- */
- public class JSONUtil {
- private Pattern pattern = Pattern.compile("\\[.\\]");
- private String str = "{\n" +
- " \"code\": \"200\", \n" +
- " \"total\": 1, \n" +
- " \"data\": [\n" +
- " {\n" +
- " \"people\":{\n" +
- " \t\"name\":\"SS\",\n" +
- " \"age\":10,\n" +
- " \"sex\":\"F\",\n" +
- " \"address\":{\n" +
- " \"country\":\"中国\",\n" +
- " \"city\":\"重庆市\",\n" +
- " \t \"street\":\"皇室桀骜\"\n" +
- " }\n" +
- " }\n" +
- " },{\n" +
- " \"people\":{\n" +
- " \t\"name\":\"SS\",\n" +
- " \"age\":10,\n" +
- " \"sex\":\"F\",\n" +
- " \"address\":{\n" +
- " \"country\":\"中国\",\n" +
- " \"city\":\"重庆市\",\n" +
- " \t \"street\":\"皇室桀骜\"\n" +
- " }\n" +
- " }\n" +
- " },{\n" +
- " \"people\":{\n" +
- " \t\"name\":\"SS\",\n" +
- " \"age\":10,\n" +
- " \"sex\":\"F\",\n" +
- " \"address\":{\n" +
- " \"country\":\"中国\",\n" +
- " \"city\":\"重庆市\",\n" +
- " \t \"street\":\"皇室桀骜\"\n" +
- " }\n" +
- " }\n" +
- " },{\n" +
- " \"people\":{\n" +
- " \t\"name\":\"SS\",\n" +
- " \"age\":10,\n" +
- " \"sex\":\"F\",\n" +
- " \"address\":{\n" +
- " \"country\":\"中国\",\n" +
- " \"city\":\"重庆市\",\n" +
- " \t \"street\":\"皇室桀骜\"\n" +
- " }\n" +
- " }\n" +
- " }\n" +
- " ]\n" +
- "}";
-
- public static void main(String[] args) {
- JSONUtil util = new JSONUtil();
- JSONObject obj = (JSONObject) JSONObject.parse(util.str);
- List<String> list = util.splitDataByPath(obj,"data[?].people.address",new ArrayList<>());
- for(String s:list){
- System.out.println(s);
- }
- }
-
- private List<String> splitDataByPath(JSONObject object, String path, List<String> list) {
- if(path == null||"".equals(path)){
- System.out.println("path 不能为空");
- }
- if(object == null){
- return list;
- }
- int poz = path.indexOf(".");
- if(poz!=-1){
- String p = path.substring(0,poz);
- String idx = regexPath(p);
- //继续封装,递归
- String nextPath = path.substring(poz+1);
- if(idx==null){
- splitDataByPath(object.getJSONObject(p),nextPath,list);
- }else{
- JSONArray array = object.getJSONArray(p.substring(0,p.length()-3));
- if("?".equals(idx)){
- //everyone should to do
- for(Object o:array){
- splitDataByPath((JSONObject)o,nextPath,list);
- }
- }else {
- //just get one
- int index = Integer.valueOf(idx);
- //防止下标越界
- if(index<array.size()){
- splitDataByPath((JSONObject) array.get(index),nextPath,list);
- }
-
- }
- }
- }else{
- String idx = regexPath(path);
- //取数据,封装返回
- if(idx==null){
- list.add(object.getString(path));
- }else{
- JSONArray array = object.getJSONArray(path.substring(0,path.length()-3));
- if("?".equals(idx)){
- //everyone should to do
- for(Object o:array){
- list.add(o.toString());
- }
- }else {
- //just get one
- int index = Integer.valueOf(idx);
- if(index<array.size()){
- list.add(array.getString(index));
- }
- }
- }
- }
- return list;
- }
-
- /**
- * 解析path,并判断是否为数组,及数组中的位置
- * @param path
- * @return
- */
- private String regexPath(String path){
- Matcher matcher = pattern.matcher(path);
- if(matcher.find()){
- String pos = matcher.group();
- return pos.substring(1,2);
- }
- return null;
- }
- }

这里使用了fastjson,也可以使用其他json替换。最终得到的结果如下:
- {"country":"中国","city":"重庆市","street":"皇室桀骜"}
- {"country":"中国","city":"重庆市","street":"皇室桀骜"}
- {"country":"中国","city":"重庆市","street":"皇室桀骜"}
- {"country":"中国","city":"重庆市","street":"皇室桀骜"}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。