Tableau json API WebConnector

时间:2016-08-24 13:31:14

标签: javascript json api iterator tableau

我已经编写了以下tableau webconnector来从内部API中提取数据,使用earthquakeUSGS.html作为指导(https://github.com/tableau/webdataconnector)。 API返回json(参见下面的代码)。我一直在使用“Web数据连接器模拟器2.0”,一切都进展顺利。我得到了正确的表,但是,我无法“获取表数据”。因为这是我的第一个js脚本,我非常确定错误是。为了遍历数据,我在这篇文章Iterate through nested json object array

中使用了Korijn的答案

问题:可能是js对象上的js迭代器。如果有人可以看看json(下面),看看我的迭代器,我会非常感激。这就是我无法获取数据的原因。

test.js

(function() {
    // Create the connector object
    var myConnector = tableau.makeConnector();

    // Define the schema
    myConnector.getSchema = function(schemaCallback) {
        var cols = [{
            id: "prog",
            alias: "PrognosisTime",
            dataType: tableau.dataTypeEnum.string
        }, {
            id: "start",
            alias: "Start",
            dataType: tableau.dataTypeEnum.date
        }, {
            id: "val",
            alias: "Value",
            dataType: tableau.dataTypeEnum.float
        }];

        var tableSchema = {
            id: "table",
            alias: "187",
            columns: cols
        };

        schemaCallback([tableSchema]);
    };

    // Download the data
    myConnector.getData = function(table, doneCallback) {
        $.getJSON("http://myapi.com/119%2C7777/Flattened?start=today&end=today&timeZone=CET&asOf=now&aggregation=None", function(resp) {
            var feat = resp.features,
                tableData = [];
                tableData.push(
                    {"table":feat.properties.table}
                    );

            // Iterate over the JSON object
            //var SeriesId = feat.SeriesId
            for(var i = 0; i <feat.DataPoints.length; i++){
                var PrognosisTime = feat.DataPoints.PrognosisTime;
                var Start = feat.DataPoints.Start;
                var Value = feat.DataPoints.Value;
            }
            table.appendRows(tableData);
            doneCallback();
        });
    };

    tableau.registerConnector(myConnector);

    // Create event listeners for when the user submits the form
    $(document).ready(function() {
        $("#submitButton").click(function() {
            tableau.connectionName = "Neas"; // This will be the data source name in Tableau
            tableau.submit(); // This sends the connector object to Tableau
        });
    });
})();
来自API的

json

[
  {
    "SeriesId": 119,
    "DataPoints": [
      {
        "PrognosisTime": null,
        "Start": "2016-08-24T00:00:00",
        "Value": 26.19
      },
      {
        "PrognosisTime": null,
        "Start": "2016-08-24T01:00:00",
        "Value": 23.9
      },
      {
        "PrognosisTime": null,
        "Start": "2016-08-24T02:00:00",
        "Value": 22.82
      }
    ]
  },
  {
    "SeriesId": 7777,
    "DataPoints": [
      {
        "PrognosisTime": null,
        "Start": "2016-08-24T00:00:00",
        "Value": 36.39
      },
      {
        "PrognosisTime": null,
        "Start": "2016-08-24T00:15:00",
        "Value": 28.81
      },
      {
        "PrognosisTime": null,
        "Start": "2016-08-24T00:30:00",
        "Value": 24.28
      }
    ]
  }
]

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

var feat = resp.features

resp是来自JSON的数组,没有任何称为功能的东西。因此,只需遍历resp(或feat = resp)并拉出DataPoints数组。