【问题标题】:Tableau json API WebConnectorTableau json API Web 连接器
【发布时间】:2016-08-24 13:31:13
【问题描述】:

我编写了以下 tableau webconnector 以从内部 API 中提取数据,使用地震USGS.html 作为指南 (https://github.com/tableau/webdataconnector)。 API 返回 json(见下面的代码)。我一直在使用“Web 数据连接器模拟器 2.0”,一切顺利。我得到了正确的表格,但是,我无法“获取表格数据”。由于这是我的第一个 js 脚本,我很确定这是错误所在。为了遍历数据,我在这篇文章中使用了 Korijn 的答案Iterate through nested json object array

问题:可能是 js 迭代器在 json 对象上。如果有人可以查看 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
      }
    ]
  }
]

【问题讨论】:

    标签: javascript json api iterator tableau-api


    【解决方案1】:

    问题出在这一行:

    var feat = resp.features
    

    resp 是来自您的 JSON 的数组,没有什么叫做特征。因此,只需遍历 resp(或 feat = resp)并取出您的 DataPoints 数组。

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多