我有一個index.html文件調用main.php返回一個json對象就好了。如何解析數組從json返回從PHP
main.php返回此
{
"products": [
{
"id": "1",
"txt": "Bar",
"image": "",
"page": ""
},
{
"id": "2",
"txt": "Car",
"image": "",
"page": ""
},
{
"id": "3",
"txt": "Far",
"image": "",
"page": ""
}
],
"success": 1
}
在Java中有json.getJSONArray(TAG_PRODUCTS);
但是....我在這裏使用的是HTML和javascript ... 所以在我的html文件中我有以下的 如何從我的json返回的數據中抽出產品數組?
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://localhost:8888/HelloWorld/main.php',
contentType: "application/json; charset=utf-8",
success: function (data) {
var names = data
$('sa_content').append('<select name="input_4">');
$.each(data, function(index, element) {
alert("index: " + index + ", element.cf_id: " + element);
});
$('sa_content').append('</select>');
},
error: function(){
$('#sa_content').append('<li>There was an error loading the feed');
}
});
當我運行index.html頁面,我得到以下警報
index = products, element = [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
和
index = success, element = 1
那麼,如何拉動產品陣列出來的JSON,並通過迭代它獲得id,txt,圖像,頁面的值?
提前感謝您的時間。 Dean-O
非常好......謝謝你清理它。像冠軍一樣工作。 –