gson读取json字符串,如何使用GSON解析此JSON字符串?

Can't figure out how to parse the following JSON string with GSON into any proper object to retrieve its data.

The string is:

{"Values":[{"Date":"2014-10-01","Value":386788.0},{"Date":"2014-11-01","Value":429131.0},{"Date":"2014-12-01","Value":215217.0},{"Date":"2015-01-01","Value":270422.0},{"Date":"2015-02-01","Value":261412.0},{"Date":"2015-03-01","Value":354668.0}]}

I figured out that square brackets mean that it's an ArrayList, but then there's another object inside.

If I try and try to iterare over the ArrayList:

Map> map = gson.fromJson(json, Map.class);

Then it complains about:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.lang.String

And if I try the opposite:

Map> map = gson.fromJson(json, Map.class);

Then it's also complaining about it:

java.util.ArrayList cannot be cast to com.google.gson.internal.LinkedTreeMap

Don't understand that format :-/

Thanks for any advice!

解决方案JsonParser parser = new JsonParser();

JsonObject object = (JsonObject)parser.parse(yourString);

for (Map.Entry entry : object.entrySet()) {

JsonArray array = entry.getValue().getAsJsonArray();

for (JsonElement elementJSON : array) {

[...]

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值