2017-02-14 51 views
-4
{"batchcomplete":"","query":{"pages":{"21721040":{"pageid":21721040,"ns":0,"title":"Stack Overflow","extract":"Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky. It was created to be a more open alternative to earlier question and answer sites such as Experts-Exchange. The name for the website was chosen by voting in April 2008 by readers of Coding Horror, Atwood's popular programming blog.\nIt features questions and answers on a wide range of topics in computer programming.\nThe website serves as a platform for users to ask and answer questions, and, through membership and active participation, to vote questions and answers up or down and edit questions and answers in a fashion similar to a wiki or Digg. Users of Stack Overflow can earn reputation points and \"badges\"; for example, a person is awarded 10 reputation points for receiving an \"up\" vote on an answer given to a question, and can receive badges for their valued contributions, which represents a kind of gamification of the traditional Q&A site or forum. All user-generated content is licensed under a Creative Commons Attribute-ShareAlike license.\nClosing questions is a main differentiation from Yahoo! Answers and a way to prevent low quality questions. The mechanism was overhauled in 2013; questions edited after being put \"on hold\" now appear in a review queue. Jeff Atwood stated in 2010 that duplicate questions are not seen as a problem but rather they constitute an advantage if such additional questions drive extra traffic to the site by multiplying relevant keyword hits in search engines.\nAs of April 2014 Stack Overflow has over 4,000,000 registered users, and it exceeded 10,000,000 questions in late August 2015. Based on the type of tags assigned to questions, the top eight most discussed topics on the site are: Java, JavaScript, C#, PHP, Android, jQuery, Python and HTML.\nStack Overflow also has a Jobs section to assist developers in finding their next opportunity. For employers, Stack Overflow provides tools to brand their business, advertise their openings on the site, and source candidates from Stack Overflow's database of developers who are open to being contacted.\n\n"}}}} 

如何在Android中解析此Wikipedia API?在Android中解析Wikipedia API

+0

它看起來像JSON 1日取鑰匙,然後值,所以使用JSON解析器。 – Biffen

+0

你到目前爲止嘗試過什麼? –

+0

使用Gson如果你有json類似pojo與你 –

回答

0

試試這個,

try { 
     JSONObject objResult = new JSONObject(response); 
     String batchComplete = objResult.getString("batchcomplete"); 

     JSONObject objQuery=objResult.getJSONObject("query"); 
     JSONObject objPages=objQuery.getJSONObject("pages"); 
     JSONObject objPagesNo=objPages.getJSONObject("21721040"); 
     String page_id = objPagesNo.getString("pageid"); 
     int ns = objPagesNo.getInt("ns"); 
     String title = objPagesNo.getString("title"); 
     String extract = objPagesNo.getString("extract"); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

OR

如果21721040不是固定的,那麼該密鑰

try { 
     JSONObject objResult = new JSONObject(response); 
     String batchComplete = objResult.getString("batchcomplete"); 

     JSONObject objQuery=objResult.getJSONObject("query"); 
     JSONObject objPages=objQuery.getJSONObject("pages"); 
     Iterator<String> keys = objPages.keys(); 
     String key=null; 
     if(keys.hasNext()){ 
      key = (String)keys.next(); // First key in your json object 
     } 
     if(key!=null) { 
      JSONObject objPagesNo = objPages.getJSONObject(key); 
      String page_id = objPagesNo.getString("pageid"); 
      int ns = objPagesNo.getInt("ns"); 
      String title = objPagesNo.getString("title"); 
      String extract = objPagesNo.getString("extract"); 
     } 

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

確實「21721040」保持不變嗎? – raktale

+0

檢查更新,如果21721040不是固定的,則獲取第1個獲取鍵,然後爲該鍵獲取值 – user2025187