Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中解析wikipediaapi_Java_Android - Fatal编程技术网

Java 在Android中解析wikipediaapi

Java 在Android中解析wikipediaapi,java,android,Java,Android,如何在Android中解析这个维基百科API?试试这个 {"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

如何在Android中解析这个维基百科API?

试试这个

{"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"}}}}

如果21721040不是固定的,则先取第一个键,然后取该键的值

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

它看起来像JSON,所以请使用JSON解析器。到目前为止您尝试了什么?如果您有JSON类似的pojo,请使用Gson。21721040是否保持不变?检查更新,如果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();
    }