JQuery中的JSON数据解析从数组开始

时间:2014-10-13 16:23:47

标签: javascript jquery json parsing

在将此问题标记为重复之前,请先阅读:) 我遇到的问题是我使用的JSON文件以数组开头,所以当我尝试解析它时,我得到了这个:

Cannot read property '0' of undefined

这是我要解析的json文件:

http://data.police.uk/api/crimes-at-location?date=2014-1&lat=52.629729&lng=-1.131592

这是我正在使用的代码:

    var link = "http://data.police.uk/api/crimes-at-location?date=2014-1&lat=52.629729&lng=-1.131592";

    $.getJSON(link, function (json) {
    // Set the variables from the results array
    var cat = json.data[0].catageroy;
        $("#location").replaceWith('Category : ', cat);
});

HELP !!!

由于

2 个答案:

答案 0 :(得分:2)

删除.data 只需使用

var cat = json[0];
console.log(cat);
console.log(cat.category);
$("#location").replaceWith('Category : ', cat.category);

答案 1 :(得分:1)

您正在尝试访问json对象的data属性,该属性不存在。您data中的json实际上是json[0].catageroy,所以{{1}}会有效。