日期:2014-05-16 浏览次数:20350 次
JAVA
StringBuffer strBuffer = new StringBuffer("[{\"ZDMC\":\"申请部门\",\"ZDSZ\":\"资产名称\",\"NUM\":\"申请购买件数\",\"PRICE\":\"资产购入单价含税\",\"MONEY\":\"资产进货金额含税\"},"); strBuffer.append("{\"ZDMC\":\"广西分公司-南宁办共同1\",\"ZDSZ\":\"空调1\",\"NUM\":\"1\",\"PRICE\":\"100\",\"MONEY\":\"200\"},"); strBuffer.append("{\"ZDMC\":\"广西分公司-南宁办共同2\",\"ZDSZ\":\"空调2\",\"NUM\":\"2\",\"PRICE\":\"100\",\"MONEY\":\"200\"},"); strBuffer.append("{\"ZDMC\":\"广西分公司-南宁办共同3\",\"ZDSZ\":\"空调3\",\"NUM\":\"3\",\"PRICE\":\"100\",\"MONEY\":\"200\"}"); strBuffer.append("]");
public class JSONUtils{ public static List<Map<String, Object>> toList(Object object) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); JSONArray jsonArray = JSONArray.fromObject(object); for (Object obj : jsonArray) { JSONObject jsonObject = (JSONObject) obj; Map<String, Object> map = new HashMap<String, Object>(); Iterator it = jsonObject.keys(); while (it.hasNext()) { String key = (String) it.next(); Object value = jsonObject.get(key); map.put((String) key, value); } list.add(map); } return list; } }
循环
List<Map<String,Object>> list = JSONUtils.toList(strBuffer.toString()); for(Map<String,Object> obj : list){ System.out.println(obj.get("ZDMC") + " " + obj.get("ZDSZ") + " " + obj.get("NUM") + " " + obj.get("PRICE") + " " + obj.get("MONEY")); }
打印结果:
申请部门 资产名称 申请购买件数 资产购入单价含税 资产进货金额含税 广西分公司-南宁办共同1 空调1 1 100 200 广西分公司-南宁办共同2 空调2 2 100 200 广西分公司-南宁办共同3 空调3 3 100 200