Java 使用谷歌 Gson 解析 Json 提要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2034643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing Json Feeds with google Gson
提问by Roch
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.
我想知道如何按项目解析 JSON 提要(例如,每个项目的 url/标题/描述)。我查看了 doc / api 但是,它对我没有帮助。
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();
}
}
采纳答案by BalusC
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 as List
properties. Any objects can be mapped as another nested Javabean property. It greatly eases further processing in Java.
取决于实际的 JSON 格式。实际上,您可以创建一个与 JSON 格式匹配的自定义 Javabean 类。在JSON任何字段可以被映射为String
,Integer
,Boolean
等JavaBean属性。任何数组都可以映射为List
属性。任何对象都可以映射为另一个嵌套的 Javabean 属性。它极大地简化了 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:
没有您身边的 JSON 字符串示例,只能猜测它的外观,因此我无法在这里给出基本示例。但是我之前在这里发布过类似的答案,您可能会发现它很有用:
Gson has also an User Guide, you may find it useful as well.
Gson 还有一个User Guide,你可能会发现它也很有用。
回答by StaxMan
I don't know if GSON can do streaming/incremental binding (I thought it did not).
我不知道 GSON 是否可以进行流/增量绑定(我认为它没有)。
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.
但是是否有特定的理由只考虑那个特定的库?其他 Java JSON 处理库确实允许进行此类处理(您可以查看其他答案的链接以了解一些想法),因为在处理大型提要时这是非常重要的功能。
回答by inder
Gson 1.4 has a new API JsonStreamParser that lets you parse multiple JSON objects one by one from a stream.
Gson 1.4 有一个新的 API JsonStreamParser,可以让你从一个流中一个一个解析多个 JSON 对象。
回答by blue01
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-
您可以为 json 对象创建相应的 java 类。整数、字符串值可以按原样映射。Json 可以这样解析-
Gson gson = new GsonBuilder().create();
Response r = gson.fromJson(jsonString, Response.class);
Here is an example- http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html
这是一个例子 - http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html