删除json 文本 中特定属性 ,遍历json文本
来源:互联网 发布:dnf手柄软件 编辑:程序博客网 时间:2024/04/27 16:30
本文解决如下问题
通过字符串直接解析出字段:
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 ;}}}
0 0
- 删除json 文本 中特定属性 ,遍历json文本
- json文本
- json添加、删除、遍历属性
- 文本JSon,转json
- JSON文本传输
- java解析json文本
- json文本传输
- 文本表格转 json
- 1.5.7 JSON 文本
- Ajax请求json文本
- 使用Json-lib生成JSON文本
- Android开发之Json-构建Json文本
- Unity3d 读取json文本数据
- 文本数据交换格式:JSON
- JQuery.getJson()解析json文本
- Boost解析json格式文本
- 网络传输文本数据格式JSON
- Boost解析json格式文本
- Ubuntu使用小错集锦
- 微信小程序之页面跳转错误
- Android ViewHolder使用
- linux内核调试环境构想
- notepad++ 查找、替换、添加
- 删除json 文本 中特定属性 ,遍历json文本
- 洛谷 P1462 通往奥格瑞玛的道路
- poj 3321_Apple Tree_树状数组+dfs
- 任意多边形面积模板
- 108. Convert Sorted Array to Binary Search Tree
- 字体大小对于offsetHeight 的影响
- Velocity报错: Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException:
- 删除数组零元素
- CSS3 双动画