从 mediawiki 获取搜索结果信息 api

Get search result info from mediawiki api

我正在尝试使用 mediawiki api 提取建议文章的信息。响应采用 JSON 格式。外观如下:

{
    "query": {
        "searchinfo": {
            "totalhits": 4152
        },
        "search": [
            {
                "ns": 0,
                "title": "Albert Einstein",
                "snippet": "&quot;<span class=\"searchmatch\">Einstein</span>&quot; redirects here. For other uses, see <span class=\"searchmatch\">Albert</span> <span class=\"searchmatch\">Einstein</span> (disambiguation) and <span class=\"searchmatch\">Einstein</span> (disambiguation). <span class=\"searchmatch\">Albert</span> <span class=\"searchmatch\">Einstein</span> (/ˈælbərt ˈaɪnʃtaɪn/; German:",
                "size": 124479,
                "wordcount": 13398,
                "timestamp": "2015-05-10T12:37:14Z"
            },
            {
                "ns": 0,
                "title": "Albert Einstein Medal",
                "snippet": "The <span class=\"searchmatch\">Albert</span> <span class=\"searchmatch\">Einstein</span> Medal is an award presented by the <span class=\"searchmatch\">Albert</span> <span class=\"searchmatch\">Einstein</span> Society in Bern. First given in 1979, the award is presented to people for &quot;scientific",
                "size": 2280,
                "wordcount": 207,
                "timestamp": "2015-04-15T03:03:46Z"
            },
            ...
}

我写了一个代码来捕获标题,但我没有得到任何结果。这是我的代码:

@Override
    protected void onPostExecute(String result) {

        if (result != null && !result.equals("")) {

            try {
                JSONArray jArray = new JSONArray();
                JSONObject jObject = null;
                try {
                    jObject = new JSONObject(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Iterator x = jObject.keys();
                while (x.hasNext()){
                    String key = (String) x.next();
                    jArray.put(jObject.get(key));
                }

                for (int i = 0; i < jArray.length(); i++) {

                    JSONObject jRes = jArray.getJSONObject(i);

                    String title = jRes.getString("title");
                    Log.d("Article_title", title);

                }
                this.progressDialog.dismiss();
            } catch (JSONException e) {
                Log.e("JSONException", "Error: " + e.toString());
            }
        }
    }

我做错了什么?

试试这个:

// your root json object
JSONObject jObject = new JSONObject(result);

// query node
JSONObject queryObject = jObject.getJSONObject("query");

// get all items under search node
JSONArray searchObjects = queryObject.getJSONArray("search");

// iterate over all search items
for(int i = 0; i < searchObjects.length(); i++) {
  // get the current object as json object
  JSONObject searchObject = searchObjects.getJSONObject(i);

  // get the title
  String title = searchObject.getString("title");

  // use the title for what you want...
}