从 JSON 获取数据
我试图从这个 JSON 字符串中获取值,但我很难实现这一点。
{"DebugLogId":"1750550","RequestId":"17505503","Result":
{"Code":"","DebugLogId":"1750550","Message":""},
"Suggestions":[
{"Ranking":"1","Score":"60","Title":"This is a test message 1"},
{"Ranking":"2","Score":"60","Title":"This is a test message 2"}
]}
什么方式最容易访问“建议”中的数据?我正在使用 GSON 模块。理想情况下,我想将它们全部放入 HashMap 中。
感谢您的任何帮助和/或建议!
感谢您的帮助!
I'm trying to get the values out of this JSON string but I'm having a hard time achieving this.
{"DebugLogId":"1750550","RequestId":"17505503","Result":
{"Code":"","DebugLogId":"1750550","Message":""},
"Suggestions":[
{"Ranking":"1","Score":"60","Title":"This is a test message 1"},
{"Ranking":"2","Score":"60","Title":"This is a test message 2"}
]}
What way would be easiest to access the data in 'Suggestions'? I'm using the GSON module. Ideally I would like to put it all in a HashMap.
Thanks for any help and/or suggestions!
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
希望这有帮助:
App.java:
Debug.java:
Result.java:
Suggestion.java:
Hope this helps:
App.java:
Debug.java:
Result.java:
Suggestion.java:
我建议您使用 flexjson 库 http://flexjson.sourceforge.net/
恕我直言,它更简单且可用的库。我第一次使用 GSON,但后来将我的所有项目都切换到了 flexjson 而不是 GSON。
I'm recommend you to use flexjson library http://flexjson.sourceforge.net/
IMHO, it more simple and usable library. I has used GSON first time, but then switched all my projects to flexjson instead of GSON.
在android中使用标准json类:
Using the standard json classes in android:
如果您可以切换库,Jackson 只需一行代码即可实现。
这会将任何 JSON 对象反序列化为仅由 Java SE 组件组成的
HashMap
。我还没有看到另一个可以做到这一点的 Java-to/from-JSON 库。使用 Gson 也可以完成同样的工作,但需要多几行代码。这是一种这样的解决方案。
(我从我的博客文章中复制了这段代码 http://programmerbruce.blogspot .com/2011/06/gson-v-jackson.html。)
If you can switch libraries, Jackson can achieve that with just one line of code.
This would deserialize any JSON object into a
HashMap
, composed of just Java SE components. I haven't yet seen another Java-to/from-JSON library that can do that.The same can be accomplished with Gson, but it requires a few more lines of code. Here's one such solution.
(I copied this code from my blog post at http://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html.)