JSONP接收不可解析的JSON

时间:2011-08-09 01:37:38

标签: javascript json jquery jsonp

接收不可解析的JSON响应。我收到了JSON的罚款,但是它的格式正确,我如何解析并说明每行包含所有值的警报。非常感谢提前!

请求开始

$.ajax({
    url: apiURL+'getLocations/category/'+ category +'/countyId/'+ countyId +'/searchString/'+ searchString +'?callback=?',
    contentType: 'application/json',
    crossDomain: true,
    dataType: 'jsonp',
    callback: callTest1(),
    complete: function(data){
        if(data.code != '400')
        {
            $.each(data, function(key, value) {
                $.each(value, function(key, val){
                    alert(data.id);
                })
            });
        }
    },
    success: callTest2()
});

请求结束

输出开始

{"Location":[{"id":0,"postcodeId":85,"categoryId":0,"location":
    "Enfield Island Village"},{"id":0,"postcodeId":44,"categoryId":0,
    "location":"Isle of Dogs"},{"id":0,"postcodeId":269,"categoryId":0,
    "location":"Isleworth"},{"id":0,"postcodeId":135,"categoryId":0,
    "location":"Islington"}]
}

输出结束

1 个答案:

答案 0 :(得分:1)

试试这个。

var data = {"Location":[{"id":0,"postcodeId":85,"categoryId":0,"location":
    "Enfield Island Village"},{"id":0,"postcodeId":44,"categoryId":0,
    "location":"Isle of Dogs"},{"id":0,"postcodeId":269,"categoryId":0,
    "location":"Isleworth"},{"id":0,"postcodeId":135,"categoryId":0,
    "location":"Islington"}]
};

$.each(data.Location, function(index, val){
    alert(val.id + " , " + val.postcodeId + " , " +val.categoryId + " , " +val.location );
});

我认为您错过了.Location部分。

如果您对硬编码值不满意,请尝试此操作。

$.each(data.Location, function(index, datum) {
    $.each(datum, function(key, value) {
        alert(key + " : " + value.toString());
    });
});

工作演示:http://jsfiddle.net/naveen/kLEA6/