【发布时间】:2018-11-15 22:33:28
【问题描述】:
我正在使用 NewsApi 将一些数据提取到我的新闻模型中。请求是RestClient.get(https://newsapi.org/v2/top-headlines?sources=financial-times&apiKey=XXXXX) { |response| },它返回一个我解析为json的数据集:
data = JSON.parse(response.body)
=>
{
"status": "ok",
"totalResults": 10,
-"articles": [
-{
-"source": {
"id": ".....",
"name": "....."
},
"author": null,
"title": "......",
"description": "......",
"url": "......",
"urlToImage": "....."
},
-{
-"source": {
"id": "financial-times",
"name": "Financial Times"
},
"author": null,
"title": "...",
"description": "...",
"url": "https://www.ft.com/content/a6a3cb08-e887-11e8-8a85-04b8afea6ea3",
"urlToImage":"..."
}
]
}
我想知道如何检索每篇文章中"title" 元素的值。
@news.title = data["articles"][1]["title"] 等等...
在任何文章中具体引用 "title" 元素的最快方法是什么?
【问题讨论】:
标签: ruby-on-rails arrays json ruby-on-rails-5