2016-08-24 4 views
1

Ich habe den folgenden tableau webconnector geschrieben, um Daten aus einer internen API zu extrahieren, mit erdbebenUSGS.html als Richtlinie (https://github.com/tableau/webdataconnector). Die API gibt json zurück (siehe Code unten). Ich habe den "Web Data Connector Simulator 2.0" benutzt und alles lief gut. Ich bekomme zwar die richtige Tabelle, kann aber keine "Tabellendaten holen". Da dies mein erstes js-Skript ist, bin ich mir sehr sicher, dass es der Fehler ist. Iterieren durch die Daten verwende ich die Antwort von Korijn in diesem Beitrag Iterate through nested json object arrayTableau json API WebConnector

Das Problem: Ist wahrscheinlich mit dem js Iterator über das JSON-Objekt. Wenn jemand einen Blick auf den JSON (unten) werfen und sich meinen Iterator anschauen kann, würde ich das sehr schätzen. Das macht es mir unmöglich, Daten zu holen.

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 
     }); 
    }); 
})(); 

json von API

[ 
    { 
    "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 
     } 
    ] 
    } 
] 

Antwort

1

Das Problem ist, diese Zeile:

var feat = resp.features 

bzw. ein Array von Ihrem JSON gibt Es gibt nichts, was man Eigenschaften nennt. Also, iterieren Sie einfach über resp (oder feat = resp) und ziehen Sie Ihr DataPoints-Array heraus.