从react.js中的Json获取数据

时间:2015-01-27 12:28:00

标签: javascript json

所以我有这样的数据

{
"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"
    },

我需要获取每个display_name的值,这些值是Print&电子书小说,印刷&电子书非小说等。

你能提出一些解决方法吗? THX

2 个答案:

答案 0 :(得分:-1)

实际上它非常简单。我想你已经将它存储在一个以例如数据命名的变量中,因此你可以通过简单的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
}
祝你好运!希望我帮忙

答案 1 :(得分:-1)

使用地图功能:https://developer.mozilla.org/en-US/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是一个使用函数映射迭代的数组。 singleResult是当前元素(在本例中为对象),其属性为“display_name”,您希望将其返回到我称为名称的新数组

相关问题