2011-08-09 44 views
2

接收不可解析的JSON響應。我收到了JSON罰款,但它是在這種確切的格式,我如何解析並說每個警報包含每行的值。提前謝謝了!JSONP接收不可解析的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"}] 
} 

輸出端

+0

以何種方式是這樣的無法分析的? – cdhowie

+0

嗯,我找不到一種方法來讀出輸出JSON中的數據,有什麼建議嗎?我對此很陌生...感謝您的耐心等待 – user885068

+0

輸出示例將非常感謝 – user885068

回答

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/

+0

JSFiddle解決的答案http://jsfiddle.net/halsaleh/v7tr9/ – user885068