使用Hashtable和List结合拼json数据
在做项目的时候,有时候需要向页面返回一个特定的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;
}
使用Hashtable和List结合拼json数据的更多相关文章
- java递归算法实现拼装树形JSON数据
有时候页面需要使用jQuery easy ui中的combotree,需要给combotree提供一个JSON数据,使用如下方法(递归)实现(下面是dao层的实现层): /** * 根据表名和父id拼 ...
- 将相关数据拼成所需JSON数据
参考: http://www.cnblogs.com/shuilangyizu/p/6019561.html 有时候我们需要将一些数据拼装成所需要格式的JSON,可以使用如下方法,本人觉得还是比较方便 ...
- 拼json对象批量向后台添加数据
网站中如果遇到批量提交格式相同数据,可以使用json来传输 $("#catalogSave").click(function(){ var array=[]; $("[n ...
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
调用jQuery的ajax方法时,jQuery会根据post或者get协议对参数data进行序列化; 如果提交的数据使用复杂的json数据,例如: {userId:32323,userName:{fi ...
- 在SQL 中生成JSON数据
这段时间接手一个数据操作记录的功能,刚拿到手上的时候打算用EF做,后来经过仔细考虑最后还是觉定放弃,最后思考再三决定: 1.以模块为单位分表.列固定(其实可以所有的操作记录都放到同一个表,但是考虑到数 ...
- 格式化JSON数据
function formatJson(json, options) { var reg = null, formatted = '', pad = 0, PADDING = ' '; options ...
- (转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...
- libcurl HTTP POST请求向服务器发送json数据【转】
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- 在java中像js那样处理json数据
工作中经常需要通过ajax向前台返回json数据,都是通过拼字符串拼出来的,很发麻烦不说,还容易出错. 于是想,能不能像js那样操作json呢?或者说更方便的操作呢? Google的gson就是这样的 ...
随机推荐
- shell脚本练习(短路练习)
#!/bin/bash #By Spinestars#2013-11-11#This is a lvsnap of auto-create Help(){ echo "Usage: ---d ...
- MVC4 + EF + System.Threading.Thread 出现的问题记录
项目要求是页面监测到后台数据库用户数据(Users)变化,前台做出相应的响应和操作. 一.参考很多资料,大概有几种方式: 参考资料地址:http://www.cnblogs.com/hoojo/p/l ...
- log4net使用(包括单个文件和按日期生成多个文件)
1.log4net生成单个文件 直接将这段代码考到config中即可用 <log4net> <!--定义输出到文件中--> <appender name="Lo ...
- 给兄弟说下如何处理Debian下常见的apache2的几个问题
这段时间总是有兄弟问到在linux下的apache2配置,其实很简单,这里统一答复下. 一.安装 当然是apt-get install 最简单了, 顺便把php5和GD什么的一起装上吧,基本上不用配置 ...
- WPF 利用子线程弹出子窗体的研究
一般来说子线程都是用来处理数据的,主窗体用来实现展现,但是有些时候我们希望子窗体实现等待效果,遮挡主窗体并使主窗体逻辑正常进行,这个业务需求虽然不多,但是正好我们用到了,于是我打算把研究成果写在这了. ...
- linux vi 中s 替换方法
vi/vim 中可以使用 :s 命令来替换字符:s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n, ...
- perl HTML::TreeBuilder::XPath
HTML::TreeBuilder::XPath 添加XPath 支持HTML::TreeBuilder use HTML::TreeBuilder::XPath; my $tree= HTML: ...
- 数据可视化的优秀入门书籍有哪些,D3.js 学习资源汇总
习·D3.js 学习资源汇总 除了D3.js自身以外,许多可视化工具包都是基于D3开发的,所以对D3的学习就显得很重要了,当然如果已经有了Javascript的经验,学起来也会不费力些. Github ...
- Linux系统编程(11)——进程间通信之有名管道
管道应用的一个重大限制是它没有名字,因此,只能用于具有亲缘关系的进程间通信,在有名管道(named pipe或FIFO)提出后,该限制得到了克服.FIFO不同于管道之处在于它提供一个路径名与之关联,以 ...
- 探索PHP+Nginx(一) 安装Linux操作系统
每次学习一种新的开发语言的时候,都要经历一个很纠结的过程,除非你运气很好或者准备工作充分,否则你在这个过程中总会耗费大量的时间和精力,当然你也会受益很多.而这个过程就是,开发环境的基础搭建,看似是装几 ...