2013-06-27 19 views
0

從應對複雜的JSON所有值我試圖從 http://www.google.com/dictionary/json?callback=dic&q=keyword&sl=en&tl=en&restrict=pr%2Cde&client=te提取的java

提取所有的值,我想用GSON,但我不能想出什麼用它做。

是否有任何與jericohtml解析器類似的東西可以簡單地提取所有值?

另外,我需要使用java。 我可以檢索JSON,但它不是定期格式化(我的意思是一行一行寫的)

感謝

URL u=new URL(url); 
    HttpURLConnection conn = (HttpURLConnection) u.openConnection(); 
    conn.setRequestMethod("GET"); 
    conn.setRequestProperty("Accept", "application/atom+xml"); 
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0"); 

    BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    String temp=br.readLine(); 
    String strJson=temp.substring("dic(".length(),temp.length()-",200,null)".length()); 

    System.out.println(strJson); 
+0

顯示你的努力.. – commit

回答

1
您可以在GSON提取您的JSONObject並設置用戶定義的對象 click here例如

Gson gson = new Gson(); 

    try { 

     BufferedReader br = new BufferedReader(
      new FileReader("c:\\file.json")); 

     //convert the json string back to object 
     DataObject obj = gson.fromJson(br, DataObject.class); 

     System.out.println(obj); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

但我需要提取值,而不是從字符串轉換爲對象 – user2324644