2017-02-05 5 views
0

Ich versuche, Json-Daten aus einem API-Feed zu analysieren, kann aber JSON nicht erhalten, um das Array entsprechend zu analysieren.jQuery JSON Parse - Unerwarteter Token-Fehler mit getjson

Ich ziemlich neu zu diesem so bitte mit mir.

Also hier ist meine JSON-Datei, es gibt mehr dazu, aber ich wollte nicht die gesamte Datei veröffentlichen.

json Lage https://cbpfapi.unocha.org/vo1/odata/Poolfund

{ 
"odata.metadata": "https://cbpfapi.unocha.org/vo1/odata/$metadata#Poolfund", 
"value": [{ 
    "Id": 23, 
    "PoolfundName": "Afghanistan", 
    "PoolfundCodeAbbrv": "AFG23", 
    "Latitude": "34.53333300", 
    "Longitude": "69.16666700", 
    "CountryCode": "AF" 
}, { 
    "Id": 17, 
    "PoolfundName": "CAR", 
    "PoolfundCodeAbbrv": "CAR17", 
    "Latitude": "4.36122000", 
    "Longitude": "18.55496000", 
    "CountryCode": "CF" 
}, { 
    "Id": 24, 
    "PoolfundName": "DRC", 
    "PoolfundCodeAbbrv": "DRC24", 
    "Latitude": "-4.32758000", 
    "Longitude": "15.31357000", 
    "CountryCode": "CD" 
}, { 
    "Id": 15, 
    "PoolfundName": "Sudan", 
    "PoolfundCodeAbbrv": "SUD15", 
    "Latitude": "15.55177000", 
    "Longitude": "32.53241000", 
    "CountryCode": "SD" 
}, { 
    "Id": 21, 
    "PoolfundName": "Somalia", 
    "PoolfundCodeAbbrv": "SOM21", 
    "Latitude": "2.03333300", 
    "Longitude": "45.35000000", 
    "CountryCode": "SO" 
}, { 
    "Id": 19, 
    "PoolfundName": "South Sudan", 
    "PoolfundCodeAbbrv": "SSD19", 
    "Latitude": "4.85165000", 
    "Longitude": "31.58247000", 
    "CountryCode": "SS" 
}, { 
    "Id": 52, 
    "PoolfundName": "Colombia", 
    "PoolfundCodeAbbrv": "COL52", 
    "Latitude": "4.59805600", 
    "Longitude": "-74.07583300", 
    "CountryCode": "CO" 
}, { 
    "Id": 53, 
    "PoolfundName": "Ethiopia", 
    "PoolfundCodeAbbrv": "ETH53", 
    "Latitude": "8.98060340", 
    "Longitude": "38.75776050", 
    "CountryCode": "ET" 
}, { 
    "Id": 54, 
    "PoolfundName": "Haiti", 
    "PoolfundCodeAbbrv": "HTI54", 
    "Latitude": "18.53333300", 
    "Longitude": "-72.33333300", 
    "CountryCode": "HT" 
}, { 
    "Id": 59, 
    "PoolfundName": "Myanmar", 
    "PoolfundCodeAbbrv": "MMR59", 
    "Latitude": "19.74500000", 
    "Longitude": "96.12972000", 
    "CountryCode": "MM" 
}, { 
    "Id": 60, 
    "PoolfundName": "Pakistan", 
    "PoolfundCodeAbbrv": "PAK60", 
    "Latitude": "33.72938820", 
    "Longitude": "73.04329000", 
    "CountryCode": "PK" 
}, { 
    "Id": 64, 
    "PoolfundName": "Yemen", 
    "PoolfundCodeAbbrv": "YEM64", 
    "Latitude": "15.35202900", 
    "Longitude": "44.20745600", 
    "CountryCode": "YE" 
}, { 
    "Id": 67, 
    "PoolfundName": "oPt", 
    "PoolfundCodeAbbrv": "PSE67", 
    "Latitude": "31.89964000", 
    "Longitude": "35.20422000", 
    "CountryCode": "PS" 
}, { 
    "Id": 70, 
    "PoolfundName": "Turkey", 
    "PoolfundCodeAbbrv": "TUR70", 
    "Latitude": "41.01384000", 
    "Longitude": "28.94966000", 
    "CountryCode": "TR" 
}, { 
    "Id": 71, 
    "PoolfundName": "Lebanon", 
    "PoolfundCodeAbbrv": "LBN71", 
    "Latitude": "33.88894000", 
    "Longitude": "35.49442000", 
    "CountryCode": "LB" 
}, { 
    "Id": 73, 
    "PoolfundName": "Jordan", 
    "PoolfundCodeAbbrv": "JOR73", 
    "Latitude": "31.95522000", 
    "Longitude": "35.94503000", 
    "CountryCode": "JO" 
}, { 
    "Id": 62, 
    "PoolfundName": "Syria", 
    "PoolfundCodeAbbrv": "SYR62", 
    "Latitude": "33.51020000", 
    "Longitude": "36.29128000", 
    "CountryCode": "SY" 
}, { 
    "Id": 72, 
    "PoolfundName": "Iraq", 
    "PoolfundCodeAbbrv": "IRQ72", 
    "Latitude": "33.31711800", 
    "Longitude": "44.36323700", 
    "CountryCode": "IQ" 
}] 
} 

Hier ist mein Code:

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

    // Define the schema 
    myConnector.getSchema = function(schemaCallback) { 
     var cols = [{ 
      id: "Id", 
      alias: "Id", 
      dataType: tableau.dataTypeEnum.string 
     }, { 
      id: "PoolfundName", 
      alias: "PoolfundName", 
      dataType: tableau.dataTypeEnum.string 
     }, { 
      id: "PoolfundCodeAbbrv", 
      alias: "PoolfundCodeAbbrv", 
      dataType: tableau.dataTypeEnum.string 
     }, { 
      id: "Latitude", 
      alias: "Latitude", 
      dataType: tableau.dataTypeEnum.float 
     }, { 
      id: "Longitude", 
      alias: "Longitude", 
      dataType: tableau.dataTypeEnum.float 
     }, { 
      id: "CountryCode", 
      alias: "CountryCode", 
      dataType: tableau.dataTypeEnum.string 
     }]; 

     var tableSchema = { 
      id: "CBPFPoolfund", 
      alias: "CBPF Poolfund API", 
      columns: cols 
     }; 

     schemaCallback([tableSchema]); 
    }; 

    // Download the data 
    myConnector.getData = function(table, doneCallback) { 
     $.getJSON("https://cbpfapi.unocha.org/vo1/odata/Poolfund" + "?callback=?", function(resp) { 
      var val = resp.value, 
       tableData = []; 

      // Iterate over the JSON object 
      for (var i = 0, len = val.length; i < len; i++) { 
       tableData.push({ 
        "Id": val[i].properties.Id, 
        "PoolfundName": val[i].properties.PoolfundName, 
        "PoolfundCodeAbbrv": val[i].properties.PoolfundCodeAbbrv, 
        "Longitude": val[i].properties.coordinates[0], 
        "Latitude": val[i].properties.coordinates[1], 
        "CountryCode": val[i].properties.CountryCode, 
       }); 
      } 

      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 = "CBPF Poolfund API"; // This will be the data source name in Tableau 
      tableau.submit(); // This sends the connector object to Tableau 
     }); 
    }); 
})(); 

Dank!

+1

api nicht cors aktiviert ist und dient nicht JSONP. Will einen Proxy verwenden – charlietfl

+0

Aber mir wurde gesagt, dass tatsächlich ist cors anabled. im Zweifel habe ich hinzugefügt? Callback =? – toto

+0

@charlietfl tatsächlich ist es CORS aktiviert. Überprüfen Sie die Geige in meiner Antwort (eine Tabelle mit den Daten gefüllt) – mrlew

Antwort

1

fand ich einige Probleme in Ihrem Code in myConnector.getData:

  1. diese URL https://cbpfapi.unocha.org/vo1/odata/Poolfund?callback=? eine unexpected error verursacht. Das Entfernen des ?=callback=? löste dieses Problem.
  2. Sie versuchen, die Werte mit .properties.keyName zu erhalten. Sie brauchen .properties nicht. Verwenden Sie einfach: obj.keyName, um auf einen Objektschlüssel zuzugreifen.
  3. val[i].coordinates[0] und val[i].coordinates[1] ist in Ihrem Datensatz nicht vorhanden. Es ist val[i].Longitude und val[i].Latitude.

Eine Arbeitscode:

$.getJSON("https://cbpfapi.unocha.org/vo1/odata/Poolfund", function(resp) { 
    var val = resp.value, 
    tableData = []; 

    // Iterate over the JSON object 
    for (var i = 0, len = val.length; i < len; i++) { 
     tableData.push({ 
      "Id": val[i].Id, 
      "PoolfundName": val[i].PoolfundName, 
      "PoolfundCodeAbbrv": val[i].PoolfundCodeAbbrv, 
      "Longitude": val[i].Longitude, 
      "Latitude": val[i].Latitude, 
      "CountryCode": val[i].CountryCode, 
     }); 
    } 

    table.appendRows(tableData); 
    doneCallback();  

}); 

Fiddle hier: https://jsfiddle.net/mrlew/mvmmppng/3/ (eine Tabelle mit den Daten gefüllt)

+0

Erstaunliche Arbeit Danke @mrlew! Könnten Sie zu Ihrer Erklärung zurückkehren? 1 -Warum würde '? = Rückruf =?' gibt Fehler zurück. soll bei Bedarf nicht mit cors umgehen? – toto

+1

@toto, gern geschehen! Der 'Callback'-Parameter ist für' JSONP'-Antworten und wahrscheinlich liegt dieser Fehler daran, dass die API JSONP nicht unterstützt. – mrlew