如何在Java中使用gson解码json字符串? [英] How to decode a json string with gson in java?

查看:132
本文介绍了如何在Java中使用gson解码json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json字符串(社交网络Qaiku的流).如何用Java解码? 我已经搜索过,但任何结果都对我有用. 谢谢.

I have a json string (the stream of social network Qaiku). How can I decode it in Java? I've searched but any results work for me. Thank you.

推荐答案

作为使用Gson的示例,您可以执行以下操作

As an example using Gson, you could do the following

Gson gson = new Gson();
gson.fromJson(value, type);

其中值是您的编码值.诀窍带有第二个参数-类型.您需要知道您的解码方式以及JSON将以哪种Java类型结束.

where value is your encoded value. The trick comes with the second parameter - the type. You need to know what your decoding and what Java type that JSON will end in.

以下示例显示了将JSON字符串解码为称为Table的域对象列表:

The following example shows decoding a JSON string into a list of domain objects called Table:

http://javastorage.wordpress.com/2011/03/31/how-to-decode-json-with-google-gson-library/

为此,需要将类型指定为:

In order to do that the type needs to be specified as:

Type type = new TypeToken<List<Table>>(){}.getType();

Gson在这里可用:

Gson is available here:

http://code.google.com/p/google-gson/

这篇关于如何在Java中使用gson解码json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆