getjson出现意外的令牌错误

时间:2017-02-05 04:30:40

标签: javascript jquery json api tableau

我正在尝试解析来自api feed的json数据,但无法让json相应地解析数组。

所以这是我的json文件,还有更多,但我不想发布整个文件。

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

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

我在myConnector.getData内的代码中发现了一些问题:

  1. 此网址https://cbpfapi.unocha.org/vo1/odata/Poolfund?callback=?导致unexpected error。删除?=callback=?解决了此问题。
  2. 您正在尝试使用.properties.keyName获取值。你不需要.properties。只需使用:obj.keyName即可访问对象密钥。
  3. val[i].coordinates[0]val[i].coordinates[1]并不存在于您的数据集中。它是val[i].Longitudeval[i].Latitude
  4. 工作代码:

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

    这里小提琴:https://jsfiddle.net/mrlew/mvmmppng/3/(用数据填充表格)

相关问题