簡體   English   中英

使用 JavaScript 從 JSON 文件中分離多個數據

[英]Separating multiple data from a JSON file using JavaScript

我正在嘗試使用此代碼顯示從 API 獲取的 JSON 文件中的值。

const query = 'chicken';
const uri = 'https://trackapi.nutritionix.com/v2/search/instant?query=' + query;
let appid = new Headers();
appid.append('x-app-id', '19421259')
let appkey = new Headers();

appid.append('x-app-key', '54a4e6668518084478d9025d8a5e32a2')

let req = new Request(uri, {
  method: 'GET',
  headers: appid,
  appkey,
});

fetch(req)
  .then((response) => {
    if (response.ok) {
      return response.json();
    } else {
      throw new Error('BAD HTTP stuff');
    }
  })
  .then((jsonData) => {
    console.log(jsonData);
    var jsonString = JSON.stringify(jsonData);
    document.write(jsonString);

  })
  .catch((err) => {
    console.log('ERROR', err.message);
  });

它成功地返回了 JSON 並將其變成了一個字符串,但是我不知道我應該如何將它們分開,因為 JSON 包含多個數據(例如,搜索雞肉會返回多種不同品牌和烹飪類型的雞肉飯),如下所示:

{
  "common": [{
        "food_name": "chicken",
        "serving_unit": "oz",
        "tag_name": "chicken",
        "serving_qty": 3,
        "common_type": null,
        "tag_id": "9",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/9_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "chickensalad",
        "serving_unit": "cup",
        "tag_name": "chicken salad",
        "serving_qty": 0.5,
        "common_type": null,
        "tag_id": "1420",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "chicken salad",
        "serving_unit": "cup",
        "tag_name": "chicken salad",
        "serving_qty": 0.5,
        "common_type": null,
        "tag_id": "1420",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "chicken broth",
        "serving_unit": "cup",
        "tag_name": "broth chicken",
        "serving_qty": 1,
        "common_type": null,
        "tag_id": "3336",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/3336_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "whole chicken",
        "serving_unit": "chicken",
        "tag_name": "whole chicken",
        "serving_qty": 1,
        "common_type": null,
        "tag_id": "4025",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/4025_thumb.jpg"
        },
        "locale": "en_US"
      }

我應該如何實現它以便我可以分別顯示每種食物及其各自的變量?

如果我理解正確的話,您正在嘗試過濾作為 JSON 對象獲取的數據,因此它只包含“food_name”和“tag_name”屬性中保存的食物。

在這種情況下,您可以編寫一個簡單的函數來進行過濾,如下所示:

const getFilteredFoodsArray = food => {
  food = food.toLowerCase();
  return common.filter(
    data =>
      data.food_name.toLowerCase().includes(food) ||
      data.tag_name.toLowerCase().includes(food)
  );
};

然后,如果你像這樣運行它:

console.log(getFilteredFoodsArray("chicken"))

你的結果是:

[ 
  { food_name: 'chicken',
    serving_unit: 'oz',
    tag_name: 'chicken',
    serving_qty: 3,
    common_type: null,
    tag_id: '9',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/9_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'chickensalad',
    serving_unit: 'cup',
    tag_name: 'chicken salad',
    serving_qty: 0.5,
    common_type: null,
    tag_id: '1420',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'chicken salad',
    serving_unit: 'cup',
    tag_name: 'chicken salad',
    serving_qty: 0.5,
    common_type: null,
    tag_id: '1420',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'chicken broth',
    serving_unit: 'cup',
    tag_name: 'broth chicken',
    serving_qty: 1,
    common_type: null,
    tag_id: '3336',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/3336_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'whole chicken',
    serving_unit: 'chicken',
    tag_name: 'whole chicken',
    serving_qty: 1,
    common_type: null,
    tag_id: '4025',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/4025_thumb.jpg' },
    locale: 'en_US' } 
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM