我来自远程域的 JSONP 响应是:
jQuery183012824459988766945_1354016515353([{"StudentID":"BA1122","LastName":"BAG","FirstName":"RON"},{"StudentID":"B770","LastName":"BAEN","FirstName":"AI"},{"StudentID":"B994","LastName":"BALD","FirstName":"AARON"},{"StudentID":"B580","LastName":"Balzstin","FirstName":"Manda"},{"StudentID":"B932","LastName":"BAR","FirstName":"ABBAS"},{"StudentID":"B139","LastName":"BANES","FirstName":"ALAN"},{"StudentID":"B718","LastName":"Baen","FirstName":"Alex"},{"StudentID":"B524","LastName":"BAER","FirstName":"ANA"}])
我正在尝试通过以下方式使用数据:
$.getJSON('http://www.remote_domain.com/json.php?callback=?',function(res){
ParseJson(res);
function ParseJson(data){
for (var key in data) {
if (data.hasOwnProperty(key)) {
alert(key + " -> " + data[key]);
}
}
}
});
我似乎没有得到数据只是对象?
更新:
$num_columns = $rs->Fields->Count ();
$arrColumns = array();
for ($i=0; $i < $num_columns; $i++) {
$arrColumns[] = $rs->Fields($i);
$newArr[] = $rs->Fields($i)->name;
}
$arrResult = array();
while (!$rs->EOF) {
$arrRow = array();
for ($i=0; $i < $num_columns; $i++) {
$arrRow[$newArr[$i]] = $arrColumns[$i]->value;
}
$arrResult[] = $arrRow;
$rs->MoveNext();
}
echo $_GET['callback'] . '(' . json_encode($arrResult) . ')';
我的 JSON 返回如下: [{"First":"John"},{"Last":"Doe"}] 包裹在对象中的数组。我需要返回一个对象或一个数组,这样我就可以在我的客户端处理: {"First":"John"},{"Last":"Doe"}
<script>
$.getJSON('http://remote.domain.com/json.php?callback=?',function(res){
alert('Results: '+res.Last);
});
</script>
我认为这可能是我的方式 json_encode($Array); 并且不使用课程?