我目前正在使用Google Gson解析reddit.com/.json并遇到一些麻烦。 在做了一些研究后,我找到了一种方法来解析json与Gson,而不需要进行大量的课程。 我正在使用这种方法 。 这是我到目前为止的代码:
import java.io.*; import java.net.*; import com.google.gson.*; public class Subreddits { public static void main(String[] args) { URL u = null; try { u = new URL("http://www.reddit.com/.json"); } catch (MalformedURLException e) { e.printStackTrace(); } URLConnection yc = null; try { yc = u.openConnection(); } catch (IOException e) { e.printStackTrace(); } BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(yc.getInputStream())); } catch (IOException e) { e.printStackTrace(); } String json = null; StringBuilder sb = new StringBuilder(); try { while ((json = in.readLine()) != null){ sb.append(json); } } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } json = sb.toString();//String of json System.out.println(json); //I want to get [data][children][data][subreddit] JsonParser parser = new JsonParser(); JsonObject rootObj = parser.parse(json).getAsJsonObject(); JsonObject locObj = rootObj.getAsJsonObject("data").getAsJsonObject("children").getAsJsonObject("data"); String subreddit = locObj.get("subreddit").getAsString(); System.out.println(subreddit); } }I am currently parsing through reddit.com/.json with Google Gson and having some trouble. After doing some research I found a way to parse through json with Gson without making a lot of classes. I am using this method. Here is my code so far:
import java.io.*; import java.net.*; import com.google.gson.*; public class Subreddits { public static void main(String[] args) { URL u = null; try { u = new URL("http://www.reddit.com/.json"); } catch (MalformedURLException e) { e.printStackTrace(); } URLConnection yc = null; try { yc = u.openConnection(); } catch (IOException e) { e.printStackTrace(); } BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(yc.getInputStream())); } catch (IOException e) { e.printStackTrace(); } String json = null; StringBuilder sb = new StringBuilder(); try { while ((json = in.readLine()) != null){ sb.append(json); } } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } json = sb.toString();//String of json System.out.println(json); //I want to get [data][children][data][subreddit] JsonParser parser = new JsonParser(); JsonObject rootObj = parser.parse(json).getAsJsonObject(); JsonObject locObj = rootObj.getAsJsonObject("data").getAsJsonObject("children").getAsJsonObject("data"); String subreddit = locObj.get("subreddit").getAsString(); System.out.println(subreddit); } }最满意答案
您正在尝试将元素"children"作为JsonObject ,但它是一个JsonArray因为它被[ ]包围...
尝试这样的事情:
JsonParser parser = new JsonParser(); JsonObject rootObj = parser.parse(json).getAsJsonObject(); //Here is the change JsonObject locObj = rootObj .getAsJsonObject("data") .getAsJsonArray("children") .get(0) .getAsJsonObject() .getAsJsonObject("data"); String subreddit = locObj.get("subreddit").getAsString();注意:我假设您只想获取"children"数组的第一个元素的数据,因为它似乎是您想要查看代码并主要查看您的其他问题 。
You are trying to get the element "children" as a JsonObject, but it is a JsonArray because it is surrounded by [ ]...
Try something like this:
JsonParser parser = new JsonParser(); JsonObject rootObj = parser.parse(json).getAsJsonObject(); //Here is the change JsonObject locObj = rootObj .getAsJsonObject("data") .getAsJsonArray("children") .get(0) .getAsJsonObject() .getAsJsonObject("data"); String subreddit = locObj.get("subreddit").getAsString();Note: I assume that you only want to get the data of the first element of the "children" array, since it seems that it is what you want looking at your code and mainly looking at this other question of yours.
更多推荐
发布评论