Json字符串查找满足key和value的对象,打印在list中

Json字符串查找满足key和value的对象,打印在list中

json对象      parm1:{"invType":"2","purchaseFlag":"5","invQty":"999","cmmdtyCode":"761678564","supplierCode":"70057362","plantCode":"ZE99","remarks":"taopre@11.com","serialNo":"20210708191906868148"}

字符串数组  parm2:{"invType","999","cmmdtyCode","70057362"}

要求将parm1中满足parm2中值的key或者value对应的对象打印成list输出

代码如下:

 

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import java.util.*;

public class JsonTest {
    //查找满足key值等于入参数组值的对象
    public List jsonSearchKey(JSONObject object, String[] arrays) {
        String key = "";
        List list = new ArrayList<>();
        //遍历字符串数组
        for (String str : arrays) {
            key = str;
            if (object!=null) {
                //遍历json对象
                Iterator iter = object.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    Map<String,Object> map1=new HashMap<String,Object>();
                    map1.put(entry.getKey().toString(),entry.getValue());
                    //满足key值等于入参数组值的对象添加到list中
                    if (key.equals(entry.getKey().toString())) {
                        HashMap map = new HashMap();
                        map.put(entry.getKey(), entry.getValue());
                        list.add(map);
                    }
                }
            }
        }
        return list;
    }

    //查找满足value值等于入参数组值的对象
    public List jsonSearchValue(JSONObject object, String[] arrays) {
        String value = "";
        List list = new ArrayList<>();
        //遍历字符串数组
        for (String str : arrays) {
            value = str;
            if (object!=null) {
                //遍历json对象
                Iterator iter = object.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    //满足value值等于入参数组值的对象添加到list中
                    if (value.equals(entry.getValue().toString())) {
                        HashMap map = new HashMap();
                        map.put(entry.getKey(), entry.getValue());
                        list.add(map);
                    }
                }
            }
        }
        return list;
    }

    @Test
    public void fun() {
        String s1="{\"invType\":\"2\",\"purchaseFlag\":\"5\",\"invQty\":\"999\",\"cmmdtyCode\":\"761678564\",\"supplierCode\":\"70057362\",\"plantCode\":\"ZE99\",\"remarks\":\"taopre@11.com\",\"serialNo\":\"20210708191906868148\"}";
        String[] s2={"invType","999","cmmdtyCode","70057362"};
        //将s1转换成json对象
        JSONObject jo =JSONObject.parseObject(s1);
        List resultList = new ArrayList<>();
        List l1=jsonSearchKey(jo,s2);
        List l2=jsonSearchValue(jo,s2);
        for (int i=0;i<l1.size();i++){
            resultList.add(l1.get(i));
        }
        for (int i=0;i<l2.size();i++){
            resultList.add(l2.get(i));
        }
        System.out.println(resultList.toString());
    }
}

 

代码2:json入参包含子节点

import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;

import java.util.*;

public class JsonTest001 {
    public static List<Map<String, Object>> jsonSearchKey(JSONObject object, String[] arrays) {
        //对象没空 返回空
        if (object == null || arrays == null) {
            return null;
        }

        //返回集合
        List<Map<String, Object>> returnList = new ArrayList<>();

        List<String> list = new ArrayList<>();
        //遍历集合
        for (String array : arrays) {
            list.add(array);
        }

        Map<String, Object> data = new IdentityHashMap<>();

        //循环转换
        Iterator it = object.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            Object infoObjCommon = entry.getValue();
            String cn = infoObjCommon.getClass().getName();
            List<Map<String, Object>> infoList = new ArrayList<Map<String, Object>>();
            // 当返回的数据中只有一条数据时,msgTable是一个map
            if (infoObjCommon instanceof HashMap) {
                Map<String, Object> msgTableMap = (Map<String, Object>) infoObjCommon;
                infoList.add(msgTableMap);
            } else if (infoObjCommon instanceof List) {
                // 当返回的数据中有多条数据时,msgTable是一个list
                infoList = (List<Map<String, Object>>) infoObjCommon;
            } else {
                data.put(entry.getKey(), entry.getValue());
            }

            if (CollectionUtils.isNotEmpty(infoList)) {
                for (Map<String, Object> infoMap : infoList) {
                    Iterator infoMapIt = infoMap.entrySet().iterator();
                    while (infoMapIt.hasNext()) {
                        Map.Entry<String, Object> infoMapEntry = (Map.Entry<String, Object>) infoMapIt.next();
                        data.put(new String(infoMapEntry.getKey()), infoMapEntry.getValue());
                    }
                }
            }
        }

        //循环转换
        Iterator it1 = data.entrySet().iterator();
        while (it1.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it1.next();
            //String
            if (list.contains(entry.getKey()) || list.contains(entry.getValue())) {
                //map对象
                Map<String, Object> dataLast = new HashMap<>();
                dataLast.put(entry.getKey(), entry.getValue());
                returnList.add(dataLast);
            }
        }

        return returnList;
    }

    @Test
    public void fun() {
        String parm1 = "{\"supplierCode\":\"70057362\",\"productList\":[{\"invType\":\"2\",\"purchaseFlag\":\"5\",\"invQty\":\"999\",\"cmmdtyCode\":\"761678564\",\"supplierCode\":\"70057362\",\"plantCode\":\"ZE99\",\"remarks\":\"taopre@11.com\",\"serialNo\":\"20210708191906868148\"}]}";
        String[] parm2 = {"invType", "999", "cmmdtyCode", "70057362"};
        //将parm1转换成json对象
        JSONObject jo = JSONObject.parseObject(parm1);
        List<Map<String, Object>> list = jsonSearchKey(jo, parm2);
        System.out.println(list.toString());
    }

}

 

posted @ 2021-07-09 14:35  kevinFeng  阅读(462)  评论(0编辑  收藏  举报