1

我正在使用 import.io 来集成和显示我正在抓取的一系列页面。我正在使用 JS,我认为是 jQuery。

代码是这样的:https ://import.io/data/integrate/#js

<!DOCTYPE html>
<html>
<head>
    <title>Import&bull;io Example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <!-- 1. Include the client library -->
    <script src="https://cdn.import.io/js/2.0.0/importio.js"></script>
    <!-- 2. Configure the library -->
    <script type="text/javascript">
        importio.init({
            "auth": {
                "userGuid": "ee034881-d641-48e9-935d-b79819ceb83c",
                "apiKey": "YOUR_API_KEY"
            },
            "host": "import.io"
        });

        // Data and done callbacks
        var dataCallback = function(data) {
            console.log("Data received", data);
            for (var i = 0; i < data.length; i++) {
                var d = data[i];        for (var k in d.data) {
                    document.write("<i>" + k + "</i>: " + d.data[k] + "<br />");        }       > document.write("<hr>");     }
                }
                var doneCallback = function(data) {
                    console.log("Done, all data:", data);
                    document.write("<b>Done</b><hr>");
                }

    // 3. Do the query (when the function is called)
    var doQuery = function() {
          // Query for tile ladbrokes promos
          importio.query({
            "connectorGuids": [
            "f6833c85-4a92-47b1-9b96-4c287eac5d24"
            ],
            "input": {
                "webpage/url": "https://www.ladbrokes.com.au/promotions/"
            }
          }, { "data": dataCallback, "done": doneCallback });
        }
    </script>
</head>
<body>
    <button onclick="doQuery()">Query</button>
</body>
</html>

然而,该代码的结果是一个按钮,它将我带到一个新页面并为我提供数据的原始转储。如何解析数据(它看起来像 JSON?)并在页面上很好地格式化它?例如表格或分层列表?

返回的数据遵循以下格式:

   {
    "link/_title": "National Olympic Committee of the Azerbaijani Republic",
    "updated/_utc": "Tue Feb 26 13:38:00 GMT 2013",
    "title": [
        "National Olympic Committee of the Azerbaijani Republic",
        "Azerbaijani"
    ],
    "updated/_source": "13:38, 26 February 2013",
    "updated": 1361885880000,
    "link": "http://en.wikipedia.org/wiki/National_Olympic_Committee_of_the_Azerbaijani_Republic",
    "link/_source": "/wiki/National_Olympic_Committee_of_the_Azerbaijani_Republic",
    "link/_text": "National Olympic Committee of the Azerbaijani Republic"
}

我尝试搜索,但我认为我使用的术语不正确,因此找不到正确的结果。

干杯!

4

1 回答 1

3

您可以在返回的 JSON 字符串上使用JSON.parse() 。您将需要 jQuery 来实现这一点。

工作演示 - http://codepen.io/nitishdhar/pen/jdveD

看起来像这样 -

var jsonString = '{"link/_title":"National Olympic Committee of the Azerbaijani Republic","updated/_utc":"Tue Feb 26 13:38:00 GMT 2013","title":["National Olympic Committee of the Azerbaijani Republic","Azerbaijani"],"updated/_source":"13:38, 26 February 2013","updated":1361885880000,"link":"http://en.wikipedia.org/wiki/National_Olympic_Committee_of_the_Azerbaijani_Republic","link/_source":"/wiki/National_Olympic_Committee_of_the_Azerbaijani_Republic","link/_text":"National Olympic Committee of the Azerbaijani Republic"}';

var jsonObject = JSON.parse(jsonString);

现在您可以解析这个对象并访问所有被捕获的值。像这样的东西-

var title = jsonObject['link/_title'];
于 2014-05-27T01:36:31.280 回答