开发者

How to use jQuery parse this JSON?

开发者 https://www.devze.com 2023-04-11 14:04 出处:网络
I have a JS function like this $.post(url, params, getFoodJSONText, \"json\"); and the call back function like this:

I have a JS function like this

$.post(url, params, getFoodJSONText, "json");

and the call back function like this:

function getFoodJSONText(data) { parse JSON}

The return JSON like this:

{"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 try to use

$.each(data, function(i,item){         alert(item.foodname[i]);       });

but it won't works.

I have been searching this for quite a while but didn't get a good solution.

Anyone can suggest a way?

Thank you very much :D


item will be the current array value, and i the current index.

Just use item. You will also want to iterate over data.result too, not just data.


Call each with data.result:

$.each(data.result, function(i, item) {
    alert(this.foodname);
});

The iterator function will be called in the context of each member of the result array, so this will be equal to item within the function.


Try this:

function getFoodJSONText(data) {
    var food = $.parseJSON(data);

    //use food, for example:
    alert(food.result[0].foodid);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消