本文解决如下问题
通过字符串直接解析出字段:
String jsonData = "{aaa:[{\"name\":\"Michael\",\"age\":20,\"address\":{\"Long_name\":\"4long\",\"short_name\":\"4short\"},\"wife\":[{\"wifename\":\"Mike1\",\"wifena2\":\"Mike1kiss\"},{\"wifename\":\"Mike2\"},{\"wifename\":\"Mike3\"}]},{\"name\":\"Mike\",\"age\":21,\"address\":{\"Long_name\":\"1long\",\"short_name\":\"1short\"}}],\"bb\":\"sdfsdf\"}";
解析后打印出如下
root.aaa[0].name String
root.aaa[0].age Number
root.aaa[0].address.Long_name String
root.aaa[0].address.short_name String
root.aaa[0].wife[0].wifename String
root.aaa[0].wife[0].wifena2 String
root.bb String
2.通过路径删除 json中的属性
详见代码
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\"},{\"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();
recJson(element,"root");//解析
System.out.println(jsonData);
JSONObject jsonObject = JSON.parseObject(jsonData);
//1.取出命令集合
//2.解析命令
// 删除
List<Integer> l=new ArrayList<Integer>();
maxArrayLength(element,l);
System.out.println(l.size());
if(l.size()>0){
System.out.println(l.get(0));
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());
}
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 ;
}
}
/**
* 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<Integer> 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);
}else{
l.set(0, Math.max(l.get(0), ja.size())) ;
}
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 ;
}
}
}