2016-08-24 90 views
1

我已經使用seismicUSGS.html作爲指南(https://github.com/tableau/webdataconnector)編寫了以下tableau webconnector以從內部API中提取數據。該API返回json(請參閱下面的代碼)。我一直在使用「Web數據連接器模擬器2.0」,並且一切進展順利。我得到正確的表,但是,我無法「獲取表數據」。因爲這是我的第一個js腳本,所以我很確定這是錯誤。通過數據重複我以前從Korijn答案在這個崗位Iterate through nested json object arrayTableau json API WebConnector

的問題:恐怕與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 
     } 
    ] 
    } 
] 

回答

1

的問題是這一行:

var feat = resp.features 

RESP是從您的JSON陣列,還有沒有什麼叫功能。因此,只需遍歷resp(或feat = resp)並將您的DataPoints數組拉出即可。