使用Hashtable和List结合拼json数据
2016-11-07 16:07
281 查看
在做项目的时候,有时候需要向页面返回一个特定的json类型的数据,一般情况下会有下面的方法进行拼接:
这种方式在碰到更加复杂的情况下就显得力不从心了,会经常出现一些少逗号,多括号的问题,下面介绍一种方法拼接特定的json类型数据:
利用Hashtable和List结合,使用JSONObject类进行转化的方式会更加的方便,相关文章链接:JSONObject和JSONArray
1.拼出给定的json数据:
方法如下:
2.转换下面类型的json数据
方法如下:
public String chongzhiList() throws Exception { List list = new ArrayList(); if (StringUtils.isNotEmpty(psCode)) { list = totalQuantityManager.findPsList(psCode); } //创建StringBuffer类型的变量json,用于存放拼装好的json数据 StringBuffer json = new StringBuffer("["); //如果list不为空则执行if内的for循环 if(list!=null && list.size()>0){ for(int i=0;i<list.size();i++){ Object[] strs = (Object[]) list.get(i); json.append("{\"id\":" + "\"" + strs[0] + "\","); json.append("\"wuranwu\":" + "\"" + strs[1] + "\","); json.append("\"danwei\":" + "\"" + strs[2] + "\","); json.append("\"yipaifangliang\":" + "\"" + strs[3] + "\","); json.append("\"chongzhiliang\":" + "\"" + strs[4] + "\","); json.append("\"xukeliang\":" + "\"" + strs[5] + "\","); json.append("\"yuliang\":" + "\"" + strs[6] + "\","); json.append("\"icyuliang\":" + "\"" + strs[7] + "\"}"); if(i<list.size()-1){ json.append(","); } } } json.append("]"); this.jsonObject = json.toString(); return JSON; }
这种方式在碰到更加复杂的情况下就显得力不从心了,会经常出现一些少逗号,多括号的问题,下面介绍一种方法拼接特定的json类型数据:
利用Hashtable和List结合,使用JSONObject类进行转化的方式会更加的方便,相关文章链接:JSONObject和JSONArray
1.拼出给定的json数据:
{ "huaXue": [ 400, 1132, 601, 500, 120, 90, 1000 ], "anDan": [ 1320, 7000, 601, 234, 120, 50000, 20 ], "erYangHualiu": [ 1320, 1132, 601, 234, 120, 90, 20 ], "danYangHuawu": [ 1320, 1132, 601, 234, 120, 90, 20 ] }
方法如下:
public String getMonthFlow() throws Exception{ //创建List对象用来存放接收结果集 List list = new ArrayList(); list = totalQuantityManager.getMonthFlow(); //创建list1,list2,list3,list4分别用来存放每一条数据中的特定值 ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); ArrayList list3 = new ArrayList(); ArrayList list4 = new ArrayList(); //for循环用来循环每一条数据,从每一条数据中取出特定的数据放入集合中 for (int i = 0; i < list.size(); i++) { Object[] strs = (Object[]) list.get(i); list1.add(strs[1]); list2.add(strs[2]); list3.add(strs[4]); list4.add(strs[5]); } //创建一个哈希表对象,用于装载键值对形式的数据 Hashtable dic = new Hashtable(); dic.put("huaXue",list1); dic.put("anDan",list2); dic.put("erYangHualiu",list3); dic.put("danYangHuawu",list4); //利用JSONObject类将哈希表对象转换为json类型的数据 this.jsonObject=JSONObject.fromObject(dic).toString(); return JSON; }
2.转换下面类型的json数据
{ "type0": [ { "month": 1, "value": 400 }, { "month": 2, "value": 1132 }, { "month": 3, "value": 601 }, { "month": 4, "value": 500 }, { "month": 5, "value": 120 }, { "month": 6, "value": 90 }, { "month": 7, "value": 1000 } ], "type1": [ { "month": 1, "value": 400 }, { "month": 2, "value": 1132 }, { "month": 3, "value": 601 }, { "month": 4, "value": 500 }, { "month": 5, "value": 120 }, { "month": 6, "value": 90 }, { "month": 7, "value": 1000 } ], "type2": [ { "month": 1, "value": 400 }, { "month": 2, "value": 1132 }, { "month": 3, "value": 601 }, { "month": 4, "value": 500 }, { "month": 5, "value": 120 }, { "month": 6, "value": 90 }, { "month": 7, "value": 1000 } ], "type3": [ { "month": 1, "value": 400 }, { "month": 2, "value": 1132 }, { "month": 3, "value": 601 }, { "month": 4, "value": 500 }, { "month": 5, "value": 120 }, { "month": 6, "value": 90 }, { "month": 7, "value": 1000 } ] }
方法如下:
public String getMonthFlow() throws Exception{ //方法一: //月份 String month = ""; List list = new ArrayList(); list = totalQuantityManager.getMonthFlow(); ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); ArrayList list3 = new ArrayList(); ArrayList list4 = new ArrayList(); Hashtable dic1 = new Hashtable(); Hashtable dic2 = new Hashtable(); Hashtable dic3 = new Hashtable(); Hashtable dic4 = new Hashtable(); for (int i = 0; i < 4*list.size(); i++) { Object[] strs = (Object[]) list.get(i%7); String monTH = ""; if (strs[0]!=null) { monTH = strs[0].toString().substring(4); } if ("01".equals(monTH)) { month = "1"; }else if ("02".equals(monTH)) { month = "2"; }else if ("03".equals(monTH)) { month = "3"; }else if ("04".equals(monTH)) { month = "4"; }else if ("05".equals(monTH)) { month = "5"; }else if ("06".equals(monTH)) { month = "6"; }else if ("07".equals(monTH)) { month = "7"; }else if ("08".equals(monTH)) { month = "8"; }else if ("09".equals(monTH)) { month = "9"; }else if ("10".equals(monTH)) { month = "10"; }else if ("11".equals(monTH)) { month = "11"; }else if ("12".equals(monTH)) { month = "12"; } if (i/7==0) { dic1.put("month", month); dic1.put("value", strs[1]); list1.add(dic1); dic1 = new Hashtable(); }else if (i/7==1) { dic2.put("month", month); dic2.put("value", strs[2]); list2.add(dic2); dic2 = new Hashtable(); }else if (i/7==2){ dic3.put("month", month); dic3.put("value", strs[3]); list3.add(dic3); dic3 = new Hashtable(); }else if (i/7==3) { dic4.put("month", month); dic4.put("value", strs[4]); list4.add(dic4); dic4 = new Hashtable(); } } //哈希表对象(存放键值对()) Hashtable dic = new Hashtable(); dic.put("type0",list1); dic.put("type1",list2); dic.put("type2",list3); dic.put("type3",list4); this.jsonObject=JSONObject.fromObject(dic).toString(); /* //方法二: List list = new ArrayList(); list = totalQuantityManager.getMonthFlow(); Hashtable dic = new Hashtable(); for(int j=0;j<4;j++){ ArrayList list_s = new ArrayList(); //循环行 for (int i = 0; i < list.size(); i++) { Object[] strs = (Object[]) list.get(i); Hashtable dic1 = new Hashtable(); dic1.put("month", strs[0]); dic1.put("value", strs[1]); list1.add(dic1); Hashtable dic2 = new Hashtable(); dic2.put("month", strs[0]); dic2.put("value", strs[2]); Hashtable dic3 = new Hashtable(); dic3.put("month", strs[0]); dic3.put("value", strs[3]); Hashtable dic4 = new Hashtable(); dic4.put("month", strs[0]); dic4.put("value", strs[4]); } dic.put("type"+j,list_s); } this.jsonObject=JSONObject.fromObject(dic).toString();*/ return JSON; }
相关文章推荐
- 利用Jquery让返回的各类数据(string、集合(List<>)、类)以Json数据格式返回,为什么要用到result.d (JQuery ajax 返回数据 使用 .d 的问题)
- Struts2与ExtJs Grid结合使用json格式数据
- 关于ASP.NET下,JQuery+AJAX使用JSON返回对象集合List数据的总结
- 使用JSON获得服务器的数据并存放在List中
- \t\tasp.net MVC 结合 jquery.autocomplete.js使用json返回数据
- list转换为JsonArray时,使用JsonValueProcessor处理有关数据类型
- C#使用集合组织数据(HashTable、ArrayList、List<T>,Dictionary<K,V>
- 从数据库读取数据存放在List<T>中,在jsp中将其取出来。(使用JSON)
- 使用fastjson将json数据转换为List
- Struts2结合jquery使用Ajax传递Json数据
- 如何使用json-lib解析有2层嵌套的list数据,然后放到对应的bean中
- 使用AngularJS结合Highchart图表动态获取JSON格式数据
- ashx文件结合ajax使用(返回json数据)
- springMVC和easyUI结合使用(前后台通过json传递数据)的实例
- 关于使用struts2-json-plugin 插件,Json返回List对象数据为空解决方案
- unity使用SimpleJSON读取json数据转存程VO存入list内
- 使用Gson结合泛型类灵活解析JSON数据
- JSON 和 List、Map集合结合使用
- Andriod中ExpandableListView的使用结合json数据
- asp.net ajax解析json数据(运用List<Hashtable>装数据)