package com.common; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class FileUtils { private static String content = "", line = System .getProperty("line.separator");// 换行相当于\n public static String reader(File file) { FileReader read = null; BufferedReader reader = null; try { read = new FileReader(file); reader = new BufferedReader(read); StringBuffer buffer = new StringBuffer(""); content = reader.readLine(); while (content != null) { buffer.append(content).append(line); content = reader.readLine(); } return content = buffer.toString();// 返回 } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); if (read != null) read.close(); } catch (Exception e) { e.printStackTrace(); } } return "";// 没值就返回空 } }
package com.entity; import java.io.Serializable; import java.util.List; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class Button implements Serializable{ private String name; private List<SubButton> sub; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<SubButton> getSub() { return sub; } public void setSub(List<SubButton> sub) { this.sub = sub; } }
package com.entity; import java.io.Serializable; import java.util.List; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class Menu implements Serializable{ private List<Button> Button; public List<Button> getButton() { return Button; } public void setButton(List<Button> button) { Button = button; } }
package com.entity; import java.io.Serializable; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class SubButton implements Serializable{ private String type; private String name; private String url; private String key; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } }
package com.json; import java.io.File; import java.net.URL; import java.util.Iterator; import com.common.FileUtils; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class DoJson1 { public static void main(String[] args) { //读取文件的json数据这样就不会那么容易些字符串那么眼花 O(∩_∩)O哈哈~ URL URL = DoJson1.class.getClassLoader().getResource("json"); File file = new File(URL.getFile()); String jsonText=FileUtils.reader(file); String responseData=jsonText; JsonParser parser = new JsonParser(); JsonElement jsonEl = parser.parse(responseData); JsonObject jsonObjRoot = null; if (jsonEl.isJsonObject()) { jsonObjRoot = jsonEl.getAsJsonObject(); JsonElement menuEl=jsonObjRoot.get("menu");//menu节点 JsonElement buttonEl=menuEl.getAsJsonObject().get("button");//button节点 boolean flag=buttonEl.isJsonArray();//是否为Json数组 if(flag){ JsonArray jsonArray= buttonEl.getAsJsonArray();//转为数组 ///////////////最大的Menu节点/////////// for(Iterator iter=jsonArray.iterator();iter.hasNext();){ JsonObject jop=(JsonObject) iter.next(); String name=jop.get("name").getAsString(); System.out.println(name);//将最大自定义菜单的Button按钮打印出来 ///////////////////sub_button/Begin////////////// JsonElement sub_buttonEl=jop.get("sub_button");//sub_button节点 boolean flag1=sub_buttonEl.isJsonArray();//是否为Json数组 if(flag1){ JsonArray jsonArray1= sub_buttonEl.getAsJsonArray();//转为数组 String url="",key=""; for (Iterator iterator = jsonArray1.iterator(); iterator.hasNext();) { JsonObject jop1=(JsonObject) iterator.next(); String type=jop1.get("type").getAsString(); String name1=jop1.get("name").getAsString(); //判断这个url的key不是为空的时候就拿出来 if(jop1.has("url")){ url=jop1.get("url").getAsString(); } else{ key=jop1.get("key").getAsString(); } System.out.println(name1+"\t"+type+"\t"+url+"\t"+key+"\n"); } } } } } } }
package com.json; import java.io.File; import java.net.URL; import java.util.Iterator; import com.common.FileUtils; import com.entity.Button; import com.entity.SubButton; 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; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class DoJson { public static void main(String[] args) { URL URL = DoJson1.class.getClassLoader().getResource("json.txt"); File file = new File(URL.getFile()); String responseData=FileUtils.reader(file); JsonParser parser = new JsonParser(); JsonElement jsonEl = parser.parse(responseData); JsonObject jsonObjRoot = null; if (jsonEl.isJsonObject()) { jsonObjRoot = jsonEl.getAsJsonObject(); JsonElement menuEl=jsonObjRoot.get("menu");//menu节点 JsonElement buttonEl=menuEl.getAsJsonObject().get("button");//button节点 boolean flag=buttonEl.isJsonArray();//是否为Json数组 if(flag){ JsonArray jsonArray= buttonEl.getAsJsonArray();//转为数组 ///////////////最大的Menu节点/////////// for(Iterator iter=jsonArray.iterator();iter.hasNext();){ JsonObject jop=(JsonObject) iter.next(); Button b=new Gson().fromJson(jop, Button.class); System.out.println("最大的节点"+b.getName());//将最大自定义菜单的Button按钮打印出来 ///////////////////sub_button/Begin////////////// JsonElement sub_buttonEl=jop.get("sub_button");//sub_button节点 boolean flag1=sub_buttonEl.isJsonArray();//是否为Json数组 if(flag1){ JsonArray jsonArray1= sub_buttonEl.getAsJsonArray();//转为数组 String url="",key=""; for (Iterator iterator = jsonArray1.iterator(); iterator.hasNext();) { JsonObject jop1=(JsonObject) iterator.next(); SubButton subButton=new Gson().fromJson(jop1, SubButton.class);//方法二:灵活使用Gson取数据 //判断这个url的key不是为空的时候就拿出来 if(jop1.has("url")){ url=jop1.get("url").getAsString(); } else{ key=jop1.get("key").getAsString(); } System.out.println(subButton.getName()+"\t"+subButton.getType()+"\t"+url+"\t"+key+"\n"); } } } } } } }
{"menu": {"button": [ {"sub_button": [ {"type":"click","key":"A1","name":"天气预报"}, {"type":"click","key":"A2","name":"公交查询"}, {"type":"click","key":"A3","name":"地方搜索"}, {"type":"click","key":"A4","name":"互译在线"} ],"name":"生活助手" }, {"sub_button": [ {"type":"click","key":"B1","name":"音乐点播"}, {"type":"click","key":"B2","name":"经典游戏"}, {"type":"click","key":"B3","name":"美女电台"}, {"type":"click","key":"B4","name":"人脸识别"}, {"type":"click","key":"B5","name":"天文星座"} ],"name":"休闲驿站" }, {"sub_button": [ {"type":"click","key":"C1","name":"短信服务"}, {"type":"click","key":"C2","name":"新闻联播"}, {"type":"click","key":"C3","name":"影视在线"}, {"type":"click","key":"C4","name":"幽默笑话"}, {"type":"view","url":"http://weibo.com/resourceljl","name":"关于我们"} ],"name":"更多体验"} ] } }
package com.json; import java.util.Iterator; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** *@Author:jilongliang *@Email:[email protected] *@Date:2014-1-4 *@CopyRight:liangjilong *@Description: */ public class DoJson2 { public static void main(String[] args) { StringBuffer buffer=new StringBuffer(); buffer.append("{\"menu\":"); buffer.append("{\"button\":"); buffer.append("["); buffer.append("{\"name\":\"生活助手\",\"sub_button\":"); buffer.append("["); buffer.append("{\"type\":\"click\",\"name\":\"帮助手册\",\"url\":\"http://me2dorg.duapp.com/common/helper.html\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"股票财经\",\"key\":\"14\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"短信阅读\",\"key\":\"11\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"短信笑话\",\"key\":\"13\",\"sub_button\":[]}"); buffer.append("]"); buffer.append("},"); buffer.append("{\"name\":\"新闻博览\",\"sub_button\":"); buffer.append("["); buffer.append("{\"type\":\"click\",\"name\":\"新闻阅读\",\"key\":\"10\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"股票财经\",\"key\":\"14\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"短信阅读\",\"key\":\"11\",\"sub_button\":[]},"); buffer.append("{\"type\":\"view\",\"name\":\"在线帮助\",\"url\":\"http://me2dorg.duapp.com/common/helper.html\",\"sub_button\":[]}"); buffer.append("]"); buffer.append("},"); buffer.append("{\"name\":\"游戏娱乐\",\"sub_button\":"); buffer.append("["); buffer.append("{\"type\":\"click\",\"name\":\"新闻阅读\",\"key\":\"10\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"股票财经\",\"key\":\"14\",\"sub_button\":[]},"); buffer.append("{\"type\":\"click\",\"name\":\"短信阅读\",\"key\":\"11\",\"sub_button\":[]},"); buffer.append("{\"type\":\"view\",\"name\":\"帮助手册\",\"url\":\"http://me2dorg.duapp.com/common/helper.html\",\"sub_button\":[]}"); buffer.append("]"); buffer.append("}"); buffer.append("]"); buffer.append("}"); buffer.append("}"); String responseData=buffer.toString(); JsonParser parser = new JsonParser(); JsonElement jsonEl = parser.parse(responseData); JsonObject jsonObjRoot = null; if (jsonEl.isJsonObject()) { jsonObjRoot = jsonEl.getAsJsonObject(); JsonElement menuEl=jsonObjRoot.get("menu");//menu节点 JsonElement buttonEl=menuEl.getAsJsonObject().get("button");//button节点 boolean flag=buttonEl.isJsonArray();//是否为Json数组 if(flag){ JsonArray jsonArray= buttonEl.getAsJsonArray();//转为数组 ///////////////最大的Menu节点/////////// for(Iterator iter=jsonArray.iterator();iter.hasNext();){ JsonObject jop=(JsonObject) iter.next(); String name=jop.get("name").getAsString(); System.out.println(name);//将最大自定义菜单的Button按钮打印出来 ///////////////////sub_button/Begin////////////// JsonElement sub_buttonEl=jop.get("sub_button");//sub_button节点 boolean flag1=sub_buttonEl.isJsonArray();//是否为Json数组 if(flag1){ JsonArray jsonArray1= sub_buttonEl.getAsJsonArray();//转为数组 String url="",key=""; for (Iterator iterator = jsonArray1.iterator(); iterator.hasNext();) { JsonObject jop1=(JsonObject) iterator.next(); String type=jop1.get("type").getAsString(); String name1=jop1.get("name").getAsString(); //判断这个url的key不是为空的时候就拿出来 if(jop1.has("url")){ url=jop1.get("url").getAsString(); } else{ key=jop1.get("key").getAsString(); } System.out.println(name1+"\t"+type+"\t"+url+"\t"+key+"\n"); } } } } } } }