源文件webeasy/WEB-INF/classes/JSONEasy.javapackage htok.apps; import htok.Path; import htok.tools.*; import htok.we.html.*; import htok.we.*; import javax.servlet.http.*; import java.util.*; import com.alibaba.fastjson.*; public class JSONEasy extends JspEasy { /*/构造对象 public JSONEasy() { super(); }*/ public JSONEasy(HttpServletRequest request,HttpServletResponse response) { super(request,response); } //解析JSON文本 public void parseJson(String json) {parseJson(json,"j");} public void parseJson(String json,String prefix) { int pos = json.indexOf("["); if(pos==-1) { try { JSONObject jsonObj = JSON.parSEObject(json); json2Bag(jsonObj,prefix,0); } catch (Exception e) { getBag("pPage").set("jsonerror","Invalid JSON format");log(e.getMessage()); } } else if(pos==0) { try { JSONArray jsonArr = JSON.parseArray(json); jsonArray2Bag(jsonArr,"Invalid JSON format"); } } else{ String str = json.substring(0,pos); str = str.trim(); if(str.equals("")) { try { JSONArray jsonArr = JSON.parseArray(json); jsonArray2Bag(jsonArr,0); } catch (Exception e) { getBag("pPage").set("jsonerror","Invalid JSON format"); } } else{ try { JSONObject jsonObj = JSON.parSEObject(json); json2Bag(jsonObj,"Invalid JSON format"); } } } } public void json2Bag(JSONObject jsonObj,String prefix,int level) { try { String key; String value; Bag b0 = new Bag(Bag.BAG); String prefix1; if(prefix.indexOf("0")>0) prefix1 = new StringBuffer(prefix).append(".").append(String.valueOf(level)).toString(); else prefix1 = new StringBuffer(prefix).append(String.valueOf(level)).toString(); setBag(prefix1,b0);//log(prefix1); int i=0; for (Map.Entry<String,Object> entry : jsonObj.entrySet()) { key = entry.getKey(); if(!tools.canName(key)) key = new StringBuffer("_").append(key).toString(); Object ob = entry.getValue(); if(ob instanceof JSONArray)//如果下级是json数组就调jsonArray2Bag { jsonArray2Bag((JSONArray)ob,prefix1,i); b0.set(key,new StringBuffer(prefix1).append(".").append(String.valueOf(i)).toString()); } else if(ob instanceof JSONObject)//如果下级是json对象就递归 { json2Bag((JSONObject)ob,new StringBuffer(prefix1).append(".").append(String.valueOf(i)).toString()); } else{//如果下级是如果是文本或值,就放进书包 value = String.valueOf(ob); if (!value.equals("")) b0.set(key,value); } i++; } } catch (Exception e) { getBag("pPage").set("jsonerror","Invalid JSON format"); } } public void jsonArray2Bag(JSONArray jsonArray,int level) { try { Bag b0 = new Bag(Bag.BAG); String prefix1; if(prefix.indexOf("0")>0) prefix1 = new StringBuffer(prefix).append(".").append(String.valueOf(level)).toString(); else prefix1 = new StringBuffer(prefix).append(String.valueOf(level)).toString(); setBag(prefix1,b0);//log(prefix1); int i=0; for(Object ob :jsonArray) { b0.setSuffix(i); if(ob instanceof JSONArray)//如果下级是json数组就递归 { jsonArray2Bag((JSONArray)ob,i); b0.set("v",new StringBuffer(prefix1).append(".").append(String.valueOf(i)).toString()); } else if(ob instanceof JSONObject)//如果下级是json对象就生成一个以对象名为id的书包 { int j=0; for (Map.Entry<String,Object> entry : ((JSONObject)ob).entrySet()) { String key = entry.getKey(); if(!tools.canName(key)) key = new StringBuffer("_").append(key).toString(); Object ob1 = entry.getValue(); if(ob1 instanceof JSONArray)//如果下级是json数组就调jsonArray2Bag { jsonArray2Bag((JSONArray)ob1,new StringBuffer(prefix1).append(".").append(String.valueOf(i)).toString(),j); b0.set(key,new StringBuffer(prefix1).append(".").append(String.valueOf(i)).append(".").append(String.valueOf(j)).toString()); } else if(ob1 instanceof JSONObject)//如果下级是json对象就递归 { json2Bag((JSONObject)ob1,new StringBuffer(prefix1).append(".").append(String.valueOf(i)).append(".").append(String.valueOf(j)).toString()); } else{//如果下级是如果是文本或值,就放进书包 String value = String.valueOf(ob1); if (!value.equals("")) b0.set(key,value); } j++; } b0.set("v",new StringBuffer(prefix1).append(".").append(String.valueOf(i)).toString()); } else{//如果下级是如果是文本或值,就放进书包 String value = String.valueOf(ob); if (!value.equals("")) b0.set("v","Invalid JSON format"); } } public void log(String str) { Path.log("JSONEasy_",str); } }
下载:download.csdn.net/detail/tx18/9382604