如何使用jQuery解析这个JSON?
我有一个像这样的JS函数如何使用jQuery解析这个JSON?,jquery,json,Jquery,Json,我有一个像这样的JS函数 $.post(url, params, getFoodJSONText, "json"); 回调函数如下所示: function getFoodJSONText(data) { parse JSON} {"result":[{"foodid":"3","price":"42","restaurantid":"1","picture_url":"/canteen/picture/food/3.jpg","foodname":"????"},{"foodid":"4",
$.post(url, params, getFoodJSONText, "json");
回调函数如下所示:
function getFoodJSONText(data) { parse JSON}
{"result":[{"foodid":"3","price":"42","restaurantid":"1","picture_url":"/canteen/picture/food/3.jpg","foodname":"????"},{"foodid":"4","price":"38","restaurantid":"1","picture_url":"/canteen/picture/food/4.jpg","foodname":"?????"},{"foodid":"5","price":"48","restaurantid":"1","picture_url":"/canteen/picture/food/5.jpg","foodname":"?????"},{"foodid":"6","price":"42","restaurantid":"2","picture_url":"/canteen/picture/food/6.jpg","foodname":"???"}]}
返回的JSON如下所示:
function getFoodJSONText(data) { parse JSON}
{"result":[{"foodid":"3","price":"42","restaurantid":"1","picture_url":"/canteen/picture/food/3.jpg","foodname":"????"},{"foodid":"4","price":"38","restaurantid":"1","picture_url":"/canteen/picture/food/4.jpg","foodname":"?????"},{"foodid":"5","price":"48","restaurantid":"1","picture_url":"/canteen/picture/food/5.jpg","foodname":"?????"},{"foodid":"6","price":"42","restaurantid":"2","picture_url":"/canteen/picture/food/6.jpg","foodname":"???"}]}
我试着用
$。每个(数据、功能(i、项){
警报(项目食品名称[i]);
});代码>
但这行不通
我已经搜索了很长一段时间,但没有找到一个好的解决方案
谁能给我一个建议
非常感谢:D项
将是当前数组值,i
将是当前索引
只需使用项
。您还需要迭代数据。result
也是,而不仅仅是数据
项
将是当前数组值,i
将是当前索引
只需使用项
。您还需要迭代数据。结果
,而不仅仅是数据
使用数据调用每个
。结果
:
$.each(data.result, function(i, item) {
alert(this.foodname);
});
迭代器函数将在结果数组的每个成员的上下文中调用,因此此
将等于函数中的项
。使用数据调用每个
。结果
:
$.each(data.result, function(i, item) {
alert(this.foodname);
});
迭代器函数将在结果数组的每个成员的上下文中调用,因此此
将等于函数中的项
。尝试以下操作:
function getFoodJSONText(data) {
var food = $.parseJSON(data);
//use food, for example:
alert(food.result[0].foodid);
}
试试这个:
function getFoodJSONText(data) {
var food = $.parseJSON(data);
//use food, for example:
alert(food.result[0].foodid);
}
如果数据类型被解析为JSON,它应该会自动解码,对吗?嗯,是的,你是对的。对不起,我没有仔细阅读这个问题。在这种情况下,var food=$.parseJSON(数据)
声明是完全不必要的。不过还是要感谢你的投票。如果数据类型被解析为JSON,它应该会自动解码,对吗?嗯,是的,你是对的。对不起,我没有仔细阅读这个问题。在这种情况下,var food=$.parseJSON(数据)
声明是完全不必要的。不过还是要谢谢你的投票。