简体   繁体   English

getjson出现意外的令牌错误

[英]Unexpected Token Error with getjson

I'm trying to parse json data from a api feed but can't get get json to parse accordingly the array. 我正在尝试从api提要中解析json数据,但是无法获取get json来相应地解析数组。

So here's my json file, there is more to it but I didnt want to post the entire file. 所以这是我的json文件,还有更多内容,但我不想发布整个文件。

json location https://cbpfapi.unocha.org/vo1/odata/Poolfund json位置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"
}]
}

Here's my 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
        });
    });
})();

I found some problems in your code inside myConnector.getData : 我在myConnector.getData中的代码中发现了一些问题:

  1. this url https://cbpfapi.unocha.org/vo1/odata/Poolfund?callback=? 这个网址https://cbpfapi.unocha.org/vo1/odata/Poolfund?callback=? is causing a unexpected error . 导致unexpected error Removing the ?=callback=? 删除?=callback=? solved this issue. 解决了这个问题。
  2. you're trying to get the values using .properties.keyName . 您正在尝试使用.properties.keyName获取值。 You don't need .properties . 您不需要.properties Just use: obj.keyName to access an object key. 只需使用: obj.keyName即可访问对象密钥。
  3. val[i].coordinates[0] and val[i].coordinates[1] doesn't exist in your data set. val[i].coordinates[0]val[i].coordinates[1]在您的数据集中不存在。 It's val[i].Longitude and val[i].Latitude . 它是val[i].Longitudeval[i].Latitude

A working code: 工作代码:

$.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 here: https://jsfiddle.net/mrlew/mvmmppng/3/ (populated a table with the data) 在这里提琴: https : //jsfiddle.net/mrlew/mvmmppng/3/ (用数据填充表)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM