使用 google Gson 解析 Json Feed
我想知道如何按项目解析 JSON feed(例如每个项目的 url / 标题 / 描述)。我查看了 doc/api,但是它对我没有帮助。
这是我到目前为止得到的
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class ImportSources extends Job {
public void doJob() throws IOException {
String json = stringOfUrl("http://feed.test/all.json");
JsonObject jobj = new Gson().fromJson(json, JsonObject.class);
Logger.info(jobj.get("responseData").toString());
}
public static String stringOfUrl(String addr) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
URL url = new URL(addr);
IOUtils.copy(url.openStream(), output);
return output.toString();
}
}
I would like to know how to parse a JSON feed by items (eg. url / title / description for each item). I have had a look to the doc / api but, it didn't help me.
This is what I got so far
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class ImportSources extends Job {
public void doJob() throws IOException {
String json = stringOfUrl("http://feed.test/all.json");
JsonObject jobj = new Gson().fromJson(json, JsonObject.class);
Logger.info(jobj.get("responseData").toString());
}
public static String stringOfUrl(String addr) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
URL url = new URL(addr);
IOUtils.copy(url.openStream(), output);
return output.toString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
取决于实际的 JSON 格式。事实上,您可以创建一个与 JSON 格式匹配的自定义 Javabean 类。 JSON 中的任何字段都可以映射为
String
、Integer
、Boolean
等 Javabean 属性。任何数组都可以映射为List
属性。任何对象都可以映射为另一个嵌套 Javabean 属性。它极大地简化了 Java 中的进一步处理。如果没有您这边的 JSON 字符串示例,它只是猜测它会是什么样子,所以我不能在这里给出一个基本的示例。但我之前在这里发布过类似的答案,您可能会发现它很有用:
Gson 还有一个用户指南,您可能会发现它也很有用。
Depends on the actual JSON format. You can in fact just create a custom Javabean class which matches the JSON format. Any fields in JSON can be mapped as
String
,Integer
,Boolean
, etc Javabean properties. Any arrays can be mapped asList
properties. Any objects can be mapped as another nested Javabean property. It greatly eases further processing in Java.Without a JSON string example from your side, it's only guessing how it would look like, so I can't give a basic example here. But I've posted similar answers before here, you may find it useful:
Gson has also an User Guide, you may find it useful as well.
Gson 1.4 有一个新的 API JsonStreamParser,可让您从流中一一解析多个 JSON 对象。
Gson 1.4 has a new API JsonStreamParser that lets you parse multiple JSON objects one by one from a stream.
您可以为json对象创建相应的java类。整数、字符串值可以按原样映射。 Json可以这样解析 -
这是一个例子 - http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html
You can create corresponding java classes for the json objects. The integer, string values can be mapped as is. Json can be parsed like this-
Here is an example- http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html
我不知道GSON是否可以做流式/增量绑定(我以为不能)。
但是是否有特定的理由只考虑那个特定的库?其他 Java JSON 处理库确实允许这样的处理(您可以检查其他答案的链接以获得一些想法),因为在处理大型提要时这是非常重要的功能。
I don't know if GSON can do streaming/incremental binding (I thought it did not).
But is there specific reason to only consider that particular library? Other Java JSON processing libraries do allow such processing (you can check links the other answer has for some ideas), since it is quite important feature when processing large feeds.