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; } }