我在这个解析中做错了什么?(What am I doing wrong in this parsing?)

编程入门 行业动态 更新时间:2024-10-08 00:25:54
我在这个解析中做错了什么?(What am I doing wrong in this parsing?)

我目前正在使用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.

更多推荐

本文发布于:2023-07-27 08:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   错了   中做   parsing   wrong

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!