我如何访问这个JSON中的对象

我目前正在一个命令行界面显示随机引号,发现一个API消耗引号,问题是JSON返回像这样的数组

[{"ID":648,"title":"Jeff Croft","content":"<p>Do you validate other <\/p>\n","link":"https:\/\/quotesondesign.com\/jeff-croft\/","custom_meta":{"Source":"<a href=\"http:\/\/cdharrison.com\/2009\/06\/notes-from-jeff-crofts-grids-css-standards-and-tomfoolery\/\">presentation<\/a>"}}] 

我想访问的content<p>Do you validate other<\/p>这是我想要的报价

所以让我们假设你的json被返回到一个variablesRESULT:

 var resultObj = JSON.parse(RESULT); var theDataYouWant = resultObj[0].content; 

内容现在在DataYouWantvariables中。

由于这是一个JSON对象数组,您可以通过它来访问它

  var data = [ { "ID": 648, "title": "Jeff Croft", "content": "<p>Do you validate other <\/p>\n", "link": "https:\/\/quotesondesign.com\/jeff-croft\/", "custom_meta": { "Source": "<a href=\"http:\/\/cdharrison.com\/2009\/06\/notes-from-jeff-crofts-grids-css-standards-and-tomfoolery\/\">presentation<\/a>" } } ]; for(var i = 0; i < data.length; i++) { console.log(data[i].content); } 
 var A = [{"ID":648,"title":"Jeff Croft","content":"<p>Do you validate other <\/p>\n","link":"https:\/\/quotesondesign.com\/jeff-croft\/","custom_meta":{"Source":"<a href=\"http:\/\/cdharrison.com\/2009\/06\/notes-from-jeff-crofts-grids-css-standards-and-tomfoolery\/\">presentation<\/a>"}}] A.map(a => a.content) //gives: ["<p>Do you validate other </p>"] 

我喜欢这种方法,因为你的json可能很大,而且你可能需要所有的内容。

但如果你只想要第一个,你总是可以解构(es6):

 const [first] = A.map(a => a.content) first; // gives => "<p>Do you validate other </p>" 

当然,我假设的不止是“一个”数据集。 你总是可以使用[0]来获得第一个项目(就像这篇文章中提到的其他项目一样)

 A.map(a => a.content)[0] //gives: "<p>Do you validate other </p>"