简体   繁体   English

JSON:无法读取未定义的属性“键值”

[英]JSON : Cannot read property 'value of a key' of undefined

So, Im trying to deal with an API.所以,我试图处理 API。 i successful load the json(which is an array of objects) in my browser like the following我成功地在我的浏览器中加载了 json(这是一个对象数组),如下所示

0:
source: {id: null, name: "Protothema.uk"}
author: "james bond"
title: " A TITLE"
description: "A DESCRIPTION"
__proto__: Object

my code我的代码

$.getJSON("http://newsapi.org/v2/top-headlines?country=uk&category=health&apiKey=MYAPIKEY", function(data){
   //console.log(data);

   $.each(data,function(index,value){
       console.log(value);

    console.log(value[0]);       
     console.log(value[0].title)//Cannot read property 'title' of undefined
   });

});



when i try to print the whole index like console.log(value[0]);当我尝试打印整个索引时,例如console.log(value[0]); i successfully get all the objects of the index 0.我成功获取了索引为 0 的所有对象。

but when i try to print a specific value of key like console.log(value[0].title) i get Cannot read property 'title' of undefined但是当我尝试打印键的特定值时,例如console.log(value[0].title)我得到无法读取未定义的属性“标题”

im stuck for hours, what im doing wrong?我被困了几个小时,我做错了什么?

Structure of Response data :响应data的结构: 在此处输入图像描述

Based on the structure, try access title of every article:根据结构,尝试访问每篇文章的标题:

// Using JQuery.each() 
$.each(data.articles,function(index, article){
  console.log(article.title); // to access title of each article.
});

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

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