我有一个调用 main.php 的 index.html 文件,它返回一个 json 对象就好了。
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
那么如何将 products 数组从 json 中拉出并遍历它以获取 id、txt、image、page 的值?
在此先感谢您的时间。院长-O