JSON字符串解析

来源:互联网 发布:mac os x ei 编辑:程序博客网 时间:2024/06/05 04:45

将JSON字符串解析为对象

-JSON字符串为:

 {"201704":{"4227":"0","4149":"1.50"},"201703":{"4245":"1","4244":"0"}}

-解析:

String data = "{\"201704\":{\"4227\":\"0\",\"4149\":\"1.50\"},\"201703\":{\"4245\":\"1\",\"4244\":\"0\"}}";System.out.println(getPriceFromJSON(data));
private static Map<String, Map<Long, BigDecimal>> getPriceFromJSON(String data){    JSONObject allInfo = JSONObject.fromObject(data);    Map<String, Map<Long, BigDecimal>> accountPriceMap = new HashMap<String, Map<Long,BigDecimal>>();    Iterator<String> allInfoIterator = allInfo.keys();    while (allInfoIterator.hasNext()) {        String time = allInfoIterator.next();        Map<Long, BigDecimal> idAndPrice = new HashMap<Long, BigDecimal>();        accountPriceMap.put(time, idAndPrice);        JSONObject idAndPriceInfo = JSONObject.fromObject(allInfo.get(time));        Iterator<String> iterator2 = idAndPriceInfo.keys();        while (iterator2.hasNext()) {            String accountid = iterator2.next();            idAndPrice.put(new Long(accountid), new BigDecimal((String)idAndPriceInfo.get(accountid)));        }    }    return accountPriceMap;}

-结果输出:
这里写图片描述

0 0
原创粉丝点击