即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

JSON字符串解析

编程语言 ppt12138 24℃ 0评论
本文目录
[隐藏]

1.将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;
}

-结果输出:


这里写图片描述

转载请注明:CodingBlog » JSON字符串解析

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情