简体   繁体   English

在react.js中从Json获取数据

[英]Fetch data from Json in react.js

So i have data like this 所以我有这样的数据

{
"copyright": "Copyright (c) 2014 The New York Times Company.  All Rights Reserved.",
"num_results": 44,
"results": [
    {
        "display_name": "Print & E-Book Fiction",
        "list_name": "Combined Print and E-Book Fiction",
        "list_name_encoded": "combined-print-and-e-book-fiction",
        "newest_published_date": "2014-12-14",
        "oldest_published_date": "2011-02-13",
        "updated": "WEEKLY"
    },
    {
        "display_name": "Print & E-Book Nonfiction",
        "list_name": "Combined Print and E-Book Nonfiction",
        "list_name_encoded": "combined-print-and-e-book-nonfiction",
        "newest_published_date": "2014-12-14",
        "oldest_published_date": "2011-02-13",
        "updated": "WEEKLY"
    },
    {
        "display_name": "Hardcover Fiction",
        "list_name": "Hardcover Fiction",
        "list_name_encoded": "hardcover-fiction",
        "newest_published_date": "2014-12-14",
        "oldest_published_date": "2008-06-08",
        "updated": "WEEKLY"
    },

I need to take the value of each display_name which are Print & E-book Fiction , Print & E-Book NonFiction and etc.. 我需要获取每个display_name的值,它们分别是Print&E-book Fiction,Print&E-Book NonFiction等。

Can you suggest something to solve it?? 您能提出一些解决方案吗? THX 谢谢

it is very simple actually. 其实很简单。 I suppose you have stored this in a variable named for example data so you'd foreach this with simple for in loop like this 我想您已经将其存储在一个名为例如数据的变量中,因此您可以使用像这样的简单for循环来实现此目的

for (item in data.results){
  item = data.results[item];
  var foo = item.display_name;
  // here you can add your checks whether it contains your string or not
  // and do whatever you want to with the foo, there is your display_name
}

Good luck! 祝好运! Hope I helped 希望我能帮上忙

Use map function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map 使用地图功能: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

var a={
"copyright": "Copyright (c) 2014 The New York Times Company.  All Rights Reserved.",
"num_results": 44,
"results": [
    {
        "display_name": "Print & E-Book Fiction",
        "list_name": "Combined Print and E-Book Fiction",
        "list_name_encoded": "combined-print-and-e-book-fiction",
        "newest_published_date": "2014-12-14",
        "oldest_published_date": "2011-02-13",
        "updated": "WEEKLY"
    },
    {
        "display_name": "Print & E-Book Nonfiction",
        "list_name": "Combined Print and E-Book Nonfiction",
        "list_name_encoded": "combined-print-and-e-book-nonfiction",
        "newest_published_date": "2014-12-14",
        "oldest_published_date": "2011-02-13",
        "updated": "WEEKLY"
    },
    {
        "display_name": "Hardcover Fiction",
        "list_name": "Hardcover Fiction",
        "list_name_encoded": "hardcover-fiction",
        "newest_published_date": "2014-12-14",
        "oldest_published_date": "2008-06-08",
        "updated": "WEEKLY"
    }]};

console.log(a);

var names = a.results.map(function(singleResult) {
  return singleResult['display_name']
});
console.log(names);  // ["Print & E-Book Fiction", "Print & E-Book Nonfiction", "Hardcover Fiction"]

a.results is an array which you iterate using function map. a.results是一个使用函数映射进行迭代的数组。 singleResult is current element(object in this case) with property 'display_name' that you want to return to your new array which I called names singleResult是具有属性“ display_name”的当前元素(在这种情况下为对象),您想返回到我称为名称的新数组

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

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