从mediawiki api获取搜索结果信息

时间:2017-01-02 22:49:56

标签: java json mediawiki

我正在尝试使用mediawiki api提取建议文章的信息。响应采用JSON格式。在这里看起来如何:

FindFoo.cmake

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

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

Try this:

// 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...
}