接口参数替换

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

import java.util.Iterator;
import java.util.Map;

public class RepalceParm {

    public Map<String, String> paramMap;
    public String toParam(String origin) {
        String param=origin;
        for(String key:paramMap.keySet()) {
            param=param.replaceAll("\\{"+key+"\\}", paramMap.get(key));
            param.replaceAll("\\{"+key+"\\}","cs");
        }
        return param;
    }

    public void saveParam(String key,String jsonPath) {

    }


    public static StringBuffer getAllKey(JSONObject jsonObject) {
        StringBuffer stringBuffer = new StringBuffer();
        Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            stringBuffer.append(key.toString()).append("|");
            if (jsonObject.get(key) instanceof JSONObject) {
                JSONObject innerObject = (JSONObject) jsonObject.get(key);
                stringBuffer.append(getAllKey(innerObject));
            } else if (jsonObject.get(key) instanceof JSONArray) {
                JSONArray innerObject = (JSONArray) jsonObject.get(key);
                stringBuffer.append(getAllKey(innerObject));
            }
        }
        return stringBuffer;
    }

    public static StringBuffer getAllKey(JSONArray json1) {
        StringBuffer stringBuffer = new StringBuffer();
        if (json1 != null ) {
            Iterator i1 = json1.iterator();
            while (i1.hasNext()) {
                Object key = i1.next();
                if (key instanceof  JSONObject) {
                    JSONObject innerObject = (JSONObject) key;
                    stringBuffer.append(getAllKey(innerObject));
                } else if (key instanceof JSONArray) {
                    JSONArray innerObject = (JSONArray) key;
                    stringBuffer.append(getAllKey(innerObject));
                }else{
                }
            }
        }
        return stringBuffer;
    }

    private final static String st1 = "{\"username\":\"tom\",\"age\":18,\"address\":[{\"province\":\"上海市\"},{\"city\":\"上海市\"},{\"disrtict\":\"静安区\"}]}";
    private final static String st2 = "{username:\"tom\",age:18}";

    public static void main(String[] args) {
        System.out.println(st1);
        JSONObject jsonObject1 =JSONObject.parseObject(st1);
        StringBuffer stb = getAllKey(jsonObject1);
        System.out.println(stb);
}}

 

 

 

=================================================比较=====================================

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Mytest {

   public static String al="[{\n" +
           "\t\"name\": \"test1\",\n" +
           "\t\"tag\": \"mock\"\n" +
           "}, {\n" +
           "\t\"name\": \"test2\",\n" +
           "\t\"tag\": \"sec\"\n" +
           "}, {\n" +
           "\t\"name\": \"test3\",\n" +
           "\t\"tag\": \"mas\"\n" +
           "}]";

   public static String bl="{\"zhan\":\"ccc\",\n" +
           "\t\"countrycode\": \"CN\",\n" +
           "\t\"clouninfo\": [{\n" +
           "\t\t\"name\": \"test1\",\n" +
           "\t\t\"tag\": \"mock\"\n" +
           "\t}, {\n" +
           "\t\t\"name\": \"test2\",\n" +
           "\t\t\"tag\": \"sec\"\n" +
           "\t}, {\n" +
           "\t\t\"name\": \"test3\",\n" +
           "\t\t\"tag\": \"mas\"\n" +
           "\t}]\n" +
           "}";
    public static String aa="[{\n" +
            "\t\"name\": \"test1\",\n" +
            "\t\"tag\": \"mock\"\n" +
            "}, {\n" +
            "\t\"name\": \"test2\",\n" +
            "\t\"tag\": \"sec\"\n" +
            "}, {\n" +
            "\t\"name\": \"test3\",\n" +
            "\t\"tag\": \"mas\"\n" +
            "},{\n" +
            "\t\"name\": \"test3\",\n" +
            "\t\"tag\": \"mas\"\n" +
            "}]";

    public static  List<Object>jsonlist=new ArrayList<>();
    public static List<Object> getAllKey(JSONObject jsonObject,String can) {
        JSONObject jo=new JSONObject();
        List<String>yao=Arrays.asList(can.split(","));
        Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();

            if(yao.contains(key)){
                jo.put(key,jsonObject.get(key));
                }
            if (jsonObject.get(key) instanceof JSONObject) {
                JSONObject innerObject = (JSONObject) jsonObject.get(key);
                getAllKey(innerObject,can);
            } else if (jsonObject.get(key) instanceof JSONArray) {
                JSONArray innerObject = (JSONArray) jsonObject.get(key);
               getAllKey(innerObject,can);
            }else{}
        }

       if(jo.size()>0){
            jsonlist.add(jo);
           System.out.println(jsonlist.toString());
       }

        return jsonlist;
    }
    public static List<Object> getAllKey(JSONArray json1,String can) {
        if (json1 != null ) {
            Iterator i1 = json1.iterator();
            while (i1.hasNext()) {
                Object key = i1.next();

                if (key instanceof  JSONObject) {
                    JSONObject innerObject = (JSONObject) key;
                    getAllKey(innerObject,can);
                }
                else if (key instanceof JSONArray) {
                    JSONArray innerObject = (JSONArray) key;
                   getAllKey(innerObject,can);
                }
                else{}
            }
        }
        return jsonlist;
    }

    public static void main(String[] args) {

    /*JSONObject JJ=JSONObject.parseObject(bl);
        getAllKey(JJ,"tag,name");*/

        System.out.println(tese(bl).toString());

    }



    public static List  tese(String JO) {
        JSONObject JJ=new JSONObject();
        List  jl=new ArrayList();
        JSONObject  js=JSONObject.parseObject(JO);
        Iterator ii=js.keySet().iterator();
        while (ii.hasNext()){
            String key=ii.next().toString();
            JJ.put(key,js.get(key));
        }
        jl.add(JJ);
        return  jl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值