简体   繁体   English

使用 JavaScript 从 JSON 文件中分离多个数据

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

I am trying to display the values from a JSON file fetched from an API using this code.我正在尝试使用此代码显示从 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);
  });

It successfully returns the JSON and makes it into a string however I don't know how I should separate them as the JSON contains multiple data (eg a search for chicken returns multiple chicken meals with varying brands and cooking types) shown below:它成功地返回了 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"
      }

How should I implement it so that I could display each food and their respective variables separately?我应该如何实现它以便我可以分别显示每种食物及其各自的变量?

If I understood it correctly, you are trying to filter the data obtained as a JSON object so it only includes the foods that are held in "food_name" and "tag_name" properties.如果我理解正确的话,您正在尝试过滤作为 JSON 对象获取的数据,因此它只包含“food_name”和“tag_name”属性中保存的食物。

In this case, you could write a simple function to do the filtering, like so:在这种情况下,您可以编写一个简单的函数来进行过滤,如下所示:

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

Then, if you run it like this:然后,如果你像这样运行它:

console.log(getFilteredFoodsArray("chicken"))

Your result would be:你的结果是:

[ 
  { 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