Java的ArrayList的数据提取 [英] Java Arraylist Data extraction

查看:488
本文介绍了Java的ArrayList的数据提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何提取数据如下:

我想从这个数组列表中提取:

  [{ITEMNAME =原始,数= 12},{ITEMNAME =烧烤,数= 23},{= ITEMNAME CATCHUP,数= 23}]

这个数组:

  {原始:12,BBQ:23日,追赶:23}

在此先感谢!下面是用于生成HashMap中的code:

 的ArrayList<&HashMap的LT;字符串,字符串>>名单=新的ArrayList<&HashMap的LT;字符串,字符串>>();
的for(int i = 0;我7;;我++){
 HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
  map.put(ITEMNAME,芯片[I]);
  map.put(数字,chipentry [I]);
  list.add(地图);
 }


解决方案

它看起来像你想将它转换为JSON,使用谷歌GSON的http://$c$c.google.com/p/google-gson/ 它很容易

提供了简单的toJSON()和fromJson()方法,将Java对象到JSON,反之亦然

下面是我的意思是:

  GSON GSON =新GSON();
gson.toJson(地图); //其中地图是地图对象

How would you extract the data as follows:

I want to extract from this arraylist:

[{itemname=Original, number=12}, {itemname=BBQ, number=23}, {itemname=CatchUp, number=23}]

This array:

{"Original":12,"BBQ":23,"CatchUp":23}

Thanks in advance! Here's the code used to generate the hashmap:

ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
for (int i = 0; i<7;i++) {
 HashMap<String,String> map = new HashMap<String,String>();
  map.put("itemname",chips[i]);
  map.put("number",chipentry[i]);
  list.add(map);
 }

解决方案

It looks like you want to convert it to Json, using google gson http://code.google.com/p/google-gson/ its very easy

"Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa"

Here is what I mean :

Gson gson = new Gson();
gson.toJson(map); //where map is your map object

这篇关于Java的ArrayList的数据提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆