package testjson;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
public class TestJson {
public static void main(String[] args){
String jsonData = "{aaa:[{\"name\":\"Michael\",\"age\":20,\"address\":{\"Long_name\":\"4long\",\"short_name\":\"4short\"},\"wife\":[{\"wifename\":\"Mike1\",\"wifena2\":\"Mike1kiss\"},{\"wifename\":\"Mike2\",\"wifena2\":\"fdsMike1kisser\"},{\"wifename\":\"Mike3\"}]},{\"name\":\"Mike\",\"age\":21,\"address\":{\"Long_name\":\"1long\",\"short_name\":\"1short\"}}],\"bb\":\"sdfsdf\"}";
// 解析获取整个json结构集合
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonData);
JsonObject jo= element.getAsJsonObject();
//JsonArray jo = element.getAsJsonArray();
//LinkedHashMap<String,Object> elementmap=new LinkedHashMap<String,Object>();
// recJson(element,"root");//解析
// String[] s="aaa[0].age".split("\\.");
// System.out.println(s[0]);
// System.out.println(s[1]);
//
// System.out.println( "aaa[0].age".indexOf("."));
//
// System.out.println("aaa[0].age".substring("aaa[0].age".indexOf(".")));
// System.out.println("aaa[0].age".contains("[0]"));
//
// System.out.println("aaa[0].age".replaceAll("\\[0\\]", ""));
// JsonObject jsonObject= element.getAsJsonObject();
System.out.println(jsonData);
JSONObject jsonObject = JSON.parseObject(jsonData);
//1.取出命令集合
//2.解析命令
/*String[] paths ="aaa[0].age".split("\\.");
String pathadd="$";
List<Integer> arraylist=new ArrayList<Integer>();
for(int i=0;i<paths.length;i++){
if(paths[i].endsWith("[0]")){
pathadd=pathadd+"."+paths[i].substring(0, paths[i].length()-4);
int length=(Integer)JSONPath.eval(jsonObject, pathadd+".size()");
if(length>0){
arraylist.add(length);
}else{
break;
}
}
pathadd+="."+paths[i];
}
int maxint= Collections.max(arraylist);*/
// JSONPath j=new JSONPath("$.aaa.age");
// j.removeArrayItem(path, currentObject, index)
//paths
/* List<String> names = (List<String>)JSONPath.eval(jsonObject, "$.aaa[3].age");
System.out.println(jsonObject.toString());*/
/* JSONObject jsonObject = JSON.parseObject(jsonData);
Map<String, Object> hashMap = JSONPath.paths(jsonObject);
Iterator iterator =hashMap.keySet().iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}*/
// Gson g=new Gson();
//g.fromJson(json, classOfT)
// String author = JsonPath.read(json, "$.store.book[0].author");
// 删除
/* List<Integer> l=new ArrayList<Integer>();
maxArrayLength(element,l);
System.out.println(l.size());
if(l.size()>0){
System.out.println(l.get(0));
System.out.println(l.get(1));
Integer maxlength=l.get(0);
String filestr= "aaa[0].wife[0].wifena2";
for(int i=0;i<maxlength;i++){
String commend="$."+filestr.replaceAll("\\[0\\]", "["+i+"]");
JSONPath.remove(jsonObject, commend);
}
}*/
System.out.println(jsonObject.toString());
List<String> ls=new ArrayList<String>();
maxArrayLength2(element,"$","aaa[0].wife[0].wifena2",ls);
for(String c:ls){
System.out.println(c);
}
//统一删除
deleteJsonPorperty(jsonObject,"aaa[0].wife[0].wifena2");
System.out.println(jsonObject.toString());
//JSONObject jsonObject = JSON.parseObject(jsonData);
}
public static void recJson(JsonElement element,String keyname){
if(element==null||element.isJsonNull()){
return;
}else if(element.isJsonArray()){
//
JsonArray jsonArray= element.getAsJsonArray();
//for(int i=0;i<jsonArray.size();i++){
//System.out.print("[0]");
if(jsonArray.size()>0){
JsonElement jsonElement= jsonArray.get(0);
recJson(jsonElement,keyname+"[0]");
}
//}
//jsonArray.get
}else if(element.isJsonObject()){
JsonObject jsonObject =element.getAsJsonObject();
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String name = entry.getKey();
JsonElement jsonElement = entry.getValue();
//System.out.print("."+name);
// elementmap.put(name, value);
recJson(jsonElement,keyname+"."+name);
}
}else if(element.isJsonPrimitive()){
JsonPrimitive jsonPrimitive=element.getAsJsonPrimitive();
if(jsonPrimitive.isBoolean()){
System.out.print(keyname+" Boolean");
}else if(jsonPrimitive.isNumber()){
System.out.print(keyname+" Number");
}else if(jsonPrimitive.isString()){
System.out.print(keyname+" String");
}else{
System.out.print(keyname+" 未知");
}
;
System.out.println();
return ;
}
}
public static void deleteJsonPorperty(JSONObject jsonObject,String removePath){
if(removePath.contains("[")||removePath.contains("]")){
List<String> ls=new ArrayList<String>();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonObject.toJSONString());
maxArrayLength2(element,"$",removePath,ls);
for(String s:ls){
JSONPath.remove(jsonObject, s);
}
}else{
JSONPath.remove(jsonObject, removePath);
}
}
/**
* aaa[0].age
* @deprecated
* ***/
public static void removeJsonPorperty(JsonElement element,String removePath){
String removePerport=removePath.split("\\.")[0];
if(removePerport.contains("[0]")){
removePerport=removePerport.replaceAll("\\[0\\]", "");
}
if(element==null||element.isJsonNull()){
return;
}else if(element.isJsonArray()){
removePath=removePath.substring(removePath.indexOf(".")+1);
JsonArray jsonArray=element.getAsJsonArray();
for(int i=0;i<jsonArray.size();i++){
JsonElement element1= jsonArray.get(i);
removeJsonPorperty(element1,removePath);
element1.toString();
}
}else if(element.isJsonObject()){
if(removePath.indexOf(".")!=-1){
removePath=removePath.substring(removePath.indexOf(".")+1);
JsonObject jsonObject=element.getAsJsonObject();
jsonObject.entrySet();
JsonElement element1= jsonObject.get(removePerport);
removeJsonPorperty(element1,removePath);
}else{
JsonObject jsonObject=element.getAsJsonObject();
jsonObject.remove(removePath);
return;
}
}
}
public static void maxArrayLength(JsonElement element,List l){
if(element==null||element.isJsonNull()){
return;
}else if(element.isJsonArray()){
JsonArray ja= element.getAsJsonArray();
Integer length= ja.size();
/*if(l.size()<=0){
l.add(length);
l.add(1);
}else{
l.set(0, Math.max(((Integer)l.get(0)), ja.size()));
l.set(1, l.get(1)+1);
}*/
for(int i=0;i<length;i++){
maxArrayLength(ja.get(i),l);
}
}else if(element.isJsonObject()){
JsonObject j= element.getAsJsonObject();
Set<Entry<String, JsonElement>> sets= j.entrySet();
for(Entry<String, JsonElement> e:sets){
maxArrayLength(e.getValue(),l);
}
}else{
return ;
}
}
public static void maxArrayLength2(JsonElement element,
String keyname,
String deletefildstr,
List<String> deleitemcommand){
if(element==null||element.isJsonNull()){
return;
}else if(element.isJsonArray()){
//
JsonArray jsonArray= element.getAsJsonArray();
//for(int i=0;i<jsonArray.size();i++){
//System.out.print("[0]");
if(jsonArray.size()>0){
for(int i=0;i<jsonArray.size();i++){
JsonElement jsonElement= jsonArray.get(i);
maxArrayLength2(jsonElement,keyname+"["+i+"]",deletefildstr,deleitemcommand);
}
}
//}
//jsonArray.get
}else if(element.isJsonObject()){
JsonObject jsonObject =element.getAsJsonObject();
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String name = entry.getKey();
JsonElement jsonElement = entry.getValue();
//System.out.print("."+name);
// elementmap.put(name, value);
maxArrayLength2(jsonElement,keyname+"."+name,deletefildstr,deleitemcommand);
}
}else if(element.isJsonPrimitive()){
JsonPrimitive jsonPrimitive=element.getAsJsonPrimitive();
// if(jsonPrimitive.isBoolean()){
// System.out.print(keyname+"");
// }else if(jsonPrimitive.isNumber()){
// System.out.print(keyname+"");
// }else if(jsonPrimitive.isString()){
// System.out.print(keyname+"");
// }else{
// System.out.print(keyname+"");
// };
// System.out.println();
String keynamepathwithoutarrayindex =keyname.replaceAll("\\[\\d\\]", "");
String deletefildstrwithoutarrayindex =deletefildstr.replaceAll("\\[0\\]", "");
if(keynamepathwithoutarrayindex.endsWith(deletefildstrwithoutarrayindex)){
deleitemcommand.add(keyname);
}
return ;
}
}
}
deletejson
最新推荐文章于 2023-09-26 09:46:45 发布