如何将list《map》(String ,object)转化成。list!!
[{pjcj=161.0, CESHIRIQI=2011, SUOSHUQUYU=通州区}, {pjcj=166.0, CESHIRIQI=2012, SUOSHUQUYU=通州区}, {pjcj=159.5, CESHIRIQI=2014, SUOSHUQUYU=通州区}, {pjcj=180.0, CESHIRIQI=2015, SUOSHUQUYU=通州区}, {pjcj=140.5, CESHIRIQI=2016, SUOSHUQUYU=东城区}, {pjcj=167.94, CESHIRIQI=2016, SUOSHUQUYU=密云区}, {pjcj=169.45, CESHIRIQI=2016, SUOSHUQUYU=房山区}, {pjcj=161.91, CESHIRIQI=2016, SUOSHUQUYU=昌平区}, {pjcj=166.75, CESHIRIQI=2017, SUOSHUQUYU=亦庄经济开发区}, {pjcj=162.63, CESHIRIQI=2017, SUOSHUQUYU=延庆}, {pjcj=163.21, CESHIRIQI=2017, SUOSHUQUYU=怀柔区}, {pjcj=166.43, CESHIRIQI=2017, SUOSHUQUYU=石景山区}, {pjcj=164.08, CESHIRIQI=2017, SUOSHUQUYU=通州区}, {pjcj=167.53, CESHIRIQI=2017, SUOSHUQUYU=门头沟区}, {pjcj=164.37, CESHIRIQI=2017, SUOSHUQUYU=顺义区}, {pjcj=166.58, CESHIRIQI=2017, SUOSHUQUYU=首都职工体质促进中心}]
这是得到的数据,如何才能变成
series: [{
name: '通州',
data: [161.0, 166.0, 159.5, 180.0]
},
类似于这种格式,一个地区,对应一个集合。
编辑于:2017.06.01 00:58
分享
0
声望: 92
2个回答
按赞数排序
##这个可以用alibaba的fastjson来做,把字符串转化成类对象,然后做归总
##下面提供个不用fastjson的比较死的方法来做(不是很好),但也可用
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String arg[]){
List<Map<String,Object>> mapList=new ArrayList<>();
Map<String,Object> map1=new HashMap<>();
map1.put("pjcj",161.1);
map1.put("CESHIRIQI",2014);
map1.put("SUOSHUQUYU","通州区");
Map<String,Object> map2=new HashMap<>();
map2.put("pjcj",162.0);
map2.put("CESHIRIQI",2015);
map2.put("SUOSHUQUYU","通州区");
Map<String,Object> map3=new HashMap<>();
map3.put("pjcj",151.0);
map3.put("CESHIRIQI",2014);
map3.put("SUOSHUQUYU","房山区");
Map<String,Object> map4=new HashMap<>();
map4.put("pjcj",152.0);
map4.put("CESHIRIQI",2014);
map4.put("SUOSHUQUYU","房山区");
mapList.add(map1);
mapList.add(map2);
mapList.add(map3);
mapList.add(map4);
Map<String,List<Double>> areaData=new HashMap<>();
for (Map<String,Object> map:mapList) {
String areaName=(String) map.get("SUOSHUQUYU");
double data=(double)map.get("pjcj");
if(areaData.get(areaName)!=null){
List<Double> datas=areaData.get(areaName);
datas.add(data);
}else{
List<Double> datass=new ArrayList<Double>();
datass.add(data);
areaData.put(areaName,datass);
}
}
List<Map<String,Object>> series=new ArrayList<>();
for (String key: areaData.keySet()) {
Map<String,Object> map=new HashMap<>();
map.put("name",key);
map.put("data",areaData.get(key));
series.add(map);
}
String json=JSON.toJSONString(series);
System.out.print(json);
}
}
结果:[{"name":"房山区","data":[151,152]},{"name":"通州区","data":[161.1,162]}]
注意依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
发布于:2017.05.23 09:02
2
|已采纳
声望: 33
橙稚 你好,我要是再加个日期,应该怎么写啊?
你是想做一个Map("地区",List)这样的效果是吧?
你的数据:List list = new ArrayList();
Map> jsonMap = new HashMap>();
List jsonList = new ArrayList();
String name = null;
for (Object object : list) {
Map<String, String> map = (Map<String, String>)object;
name = map.keySet().toString();
String value = map.values().toString();
jsonList.add(value);
}
jsonMap.put(name, jsonList);
发布于:2017.05.23 08:16
-2
声望: 309
乔丹码得 看清楚题目咯,哥们,把所有的key做key,把所有的值做值,这样出来个jsonMap是个什么鬼,人家要的是series: [{ name: '通州', data: [161.0, 166.0, 159.5, 180.0] }。你的代码有跑过么?
橙稚 好的。我试试,Map> jsonMap这是>什么意思?
||||