2013-05-17 28 views
1

我目前使用Google Gson通過reddit.com/.json解析並遇到一些麻煩。在做了一些研究之後,我發現了一種通過使用Gson解析json而不需要製作很多類的方法。我正在使用這個method。這是我到目前爲止的代碼:我在這個解析中做錯了什麼?

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); 
    } 

} 
+0

我會發表評論,如果您不使用從GSON你可能也使用來自json.org小罐子的自動序列化的元素。它提供了像JsonObject這樣的低級對象,並且是一個較小的依賴關係。 – Deadron

回答

2

您正在試圖獲得元素"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"數組的第一個元素的數據,因爲它似乎是你想看看你的代碼,主要看this other question of yours

1

children對象返回您必須迭代的Array

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(); 
    JsonArray locObj = rootObj.getAsJsonObject("data").getAsJsonArray("children"); 

    /* Iterating children object */ 
    Iterator<JsonElement> iterator = locObj.iterator(); 

    while(iterator.hasNext()){ 
     JsonElement element = iterator.next(); 
     JsonElement subreddit = element.getAsJsonObject().getAsJsonObject("data").get("subreddit"); 
     System.out.println(subreddit.getAsString()); 
    } 
} 
1
  1. 你並不需要爲可拋出異常的每一個表情創建try..catch塊。
  2. JsonParser.parse()方法接受Reader(例如InpustStreamReader)實例,因此您不必自行閱讀JSON。
  3. root[data][children]是一個對象數組,因此您必須遍歷它們才能訪問單個對象。
  4. 我相信你想讀取所有[subredit] s到某種集合,Set我按?

    public static void main(String[] args) { 
        try { 
         Set<String> subreddits = new HashSet<>(); 
         URL url = new URL("http://www.reddit.com/.json"); 
    
         JsonParser parser = new JsonParser(); 
         JsonObject root = parser.parse(new InputStreamReader(url.openConnection().getInputStream())).getAsJsonObject(); 
         JsonArray children = root.getAsJsonObject("data").getAsJsonArray("children"); 
    
         for (int i = 0; i < children.size(); i++) { 
          String subreddit = children.get(i).getAsJsonObject().getAsJsonObject("data").get("subreddit").getAsString(); 
          subreddits.add(subreddit); 
         } 
    
         System.out.println(subreddits); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
    } 
    

此代碼返回:

[IAmA, worldnews, technology, news, todayilearned, gaming, AskReddit, movies, videos, funny, bestof, science, WTF, politics, aww, pics, atheism, Music, AdviceAnimals] 
相關問題