繁体   English   中英

API Ajax结构

[英]API Ajax structuring

我的目标:实现搜索功能以显示从USDA NDB API中提取的营养数据。

状态:部分成功

问题:美国农业部(USDA)将物件退回有些令人讨厌。 参考此屏幕快照: http ://imgur.com/a/gOKdk披萨的水是第一个关键值,牛奶具有能量。

我的问题:

例如,如果我想获得所有搜索的能量,我将如何构造我的ajax调用?

$.ajax({
        type: 'GET',
        async: false,
        url: 'http://api.nal.usda.gov/ndb/reports/?ndbno=' + ndbno[0] + '&type=b&format=json&api_key=',
        success: function(results) {
            food0 = results.report.food;
            console.log(food0);
            $("#jsGrid-nutrition").jsGrid("insertItem", {
                name: food0.name,
                kCal: food0.nutrients[1].measures[0].value,
                servingSize: food0.nutrients[1].measures[0].label,
                quantity: food0.nutrients[1].measures[0].qty,
                carbs: food0.nutrients[4].value + food0.nutrients[4].unit,
                fats: food0.nutrients[3].value + food0.nutrients[3].unit,
                saturatedFats: food0.nutrients[3].value + food0.nutrients[3].unit,
                protein: food0.nutrients[24].value + food0.nutrients[24].unit
            });

您应该循环数组并搜索特定name

var energy = '';

$.each(food0.nutrient, function () {
    if (this.name == 'Energy') {
        energy = this.value;

        return false; // == break; return true == continue;
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM