嗨,我还在学习,我需要一些帮助,我要求一些json数据(见下文),我正在努力打印每个项目的日期。但这不管用。
这是我正在使用的函数
def jsonResponse(response)
file = JSON.parse(response)
items = file['content']['content']
items.each do |item|
puts item['date']
end
end
jsonResponse(response)我得到的第一个错误是:jsonResponse中的./philPapers.rb:30:in块:未定义的方法content' for nil:NilClass (NoMethodError)
下面是来自顶部的一个大型json文件的片段:
{
"level": -1,
"content": [
{
"level": 0,
"html": "<div class='sh sh0'>Material to categorize<\/div>\n",
"content": [
{
"authors": ["Aleksander, Jason"],
"currentList": "6317",
"addToList": null,
"affiliateLinks": "",
"date": "2011",
"added": "2012-10-10 22:51:27",
"catsHTML": "<div><a class='catName' href='/browse/13th14th-century-philosophy' rel='section'>13th/14th Century Philosophy<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/medieval-and-renaissance-philosophy' rel='section'>Medieval and Renaissance Philosophy<\/a><\/div> <div><a class='catName' href='/browse/medieval-philosophy-of-religion' rel='section'>Medieval Philosophy of Religion<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/medieval-and-renaissance-philosophy' rel='section'>Medieval and Renaissance Philosophy<\/a><\/div> <div><a class='catName' href='/browse/philosophy-of-literature' rel='section'>Philosophy of Literature<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/aesthetics' rel='section'>Aesthetics<\/a><\/div> ",
"__entry__": "<a href=\"http://philpapers.org/rec/ALETPO-8\"><span class='name'>Jason Aleksander<\/span> (2011). <span class='articleTitle'>The Problem of Theophany in Paradiso 33.<\/span><\/a><span class='pubInfo'> <em class='pubName'>Essays in Medieval Studies<\/em> 27:61-78.<\/span>",
"deleted": "0",
"directLink": "<a rel=\"nofollow\" href=\"http://muse.jhu.edu/journals/essays_in_medieval_studies/v027/27.aleksander.html\" target='_blank' >Direct download<\/a>",
"sortPos": "0000000000",
"googleBooksQuery": 0,
"updated": "2012-10-10",
"elId": "eALETPO-8",
"id": "ALETPO-8"
},
{
"excerpt1": "The theory of mind that medieval philosophers inherit from Augustine is predicated on the thesis that the human mind is essentially self-reflexive. This paper examines Peter John Olivi's (1248-1298) distinctive development of this traditional Augustinian thesis. The aim of the paper is three-fold. The first is to establish that Olivi's theory of reflexive awareness amounts to a theory of phenomenal consciousness. The second is to show that, despite appearances, Olivi rejects a higher-order analysis of consciousness in favor of a same-order",
"date": "forthcoming",
"added": "2013-02-28 00:07:35",
"googleBooksQuery": 0,
"updated": "2013-02-28",
"id": "BROQOC",
"authors": ["Brower-Toland, Susan"],
"addToList": null,
"excerpt2": "theory. The third and final is to show that, on his view, consciousness is both self-intimating and infallible.",
"currentList": "6317",
"affiliateLinks": "",
"deleted": "0",
"__entry__": "<a href=\"http://philpapers.org/rec/BROQOC\"><span class='name'>Susan Brower-Toland<\/span> (forthcoming). <span class='articleTitle'>&Quot;olivi on Consciousness and Self-Knowledge: The Phenomenology, Metaphysics, and Epistemology of Mind's Reflexivity&Quot;.<\/span><\/a><span class='pubInfo'> <em class='pubName'>Oxford Studies in Medieval Philosophy<\/em>.<\/span>",
"catsHTML": "<div><a class='catName' href='/browse/13th14th-century-philosophy' rel='section'>13th/14th Century Philosophy<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/medieval-and-renaissance-philosophy' rel='section'>Medieval and Renaissance Philosophy<\/a><\/div> <div><a class='catName' href='/browse/self-consciousness-in-experience' rel='section'>Self-Consciousness in Experience<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/philosophy-of-mind' rel='section'>Philosophy of Mind<\/a><\/div> <div><a class='catName' href='/browse/self-representational-theories-of-consciousness' rel='section'>Self-Representational Theories of Consciousness<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/philosophy-of-mind' rel='section'>Philosophy of Mind<\/a><\/div> ",
"directLink": "<a rel=\"nofollow\" href=\"http://docs.google.com/file/d/0B4954JF6zDtlVVpFdnNCM0pkQVU/edit?usp=sharing\" target='_blank' >Direct download<\/a>",
"sortPos": "0000000001",
"elId": "eBROQOC"
},
{
"authors": ["Buffon, Valeria A."],
"currentList": "6317",
"addToList": null,
"affiliateLinks": "<span class='price_new'><a class='price_new' target=\"_blank\" rel=\"nofollow\" href=\"http://www.amazon.com/gp/offer-listing/9004163166?SubscriptionId=1CYYSXRPEAM0Q99H1WR2&tag=philp-20&linkCode=xm2&camp=2025&creative=386001&creativeASIN=9004163166&condition=new\">$132.19 new<\/a><\/span> <span class='price_amazon'><a class='price_amazon' target=\"_blank\" rel=\"nofollow\" href=\"http://www.amazon.com/Virtue-Ethics-Middle-Ages-Commentaries/dp/9004163166?SubscriptionId=1CYYSXRPEAM0Q99H1WR2&tag=philp-20&linkCode=xm2&camp=2025&creative=165953&creativeASIN=9004163166\">$149.00 direct from Amazon<\/a><\/span> <span class='price_used'><a class='price_used' target=\"_blank\" rel=\"nofollow\" href=\"http://www.amazon.com/gp/offer-listing/9004163166?SubscriptionId=1CYYSXRPEAM0Q99H1WR2&tag=philp-20&linkCode=xm2&camp=2025&creative=386001&creativeASIN=9004163166&condition=used\">$203.83 used<\/a><\/span> (collection) <a href=\"http://www.amazon.com/Virtue-Ethics-Middle-Ages-Commentaries/dp/9004163166%3FSubscriptionId%3D1CYYSXRPEAM0Q99H1WR2%26tag%3Dphilp-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D9004163166\">Amazon page<\/a>",
"date": "2008",
"added": "2009-01-11 23:31:13",
"catsHTML": "<div><a class='catName' href='/browse/11th12th-century-philosophy' rel='section'>11th/12th Century Philosophy<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/medieval-and-renaissance-philosophy' rel='section'>Medieval and Renaissance Philosophy<\/a><\/div> <div><a class='catName' href='/browse/13th14th-century-philosophy' rel='section'>13th/14th Century Philosophy<\/a><span class='catIn'> in <\/span><a class='catArea' href='/browse/medieval-and-renaissance-philosophy' rel='section'>Medieval and Renaissance Philosophy<\/a><\/div> ",
"__entry__": "<a href=\"http://philpapers.org/rec/BUFTSO\"><span class='name'>Valeria A. Buffon<\/span> (2008). <span class='articleTitle'>The Structure of the Soul, Intellectual Virtues, and the Ethical Ideal of Masters of Arts in Early Commentaries on the Nichomachean Ethics.<\/span><\/a><span class='pubInfo'> In István Pieter Bejczy (ed.), <em>Virtue Ethics in the Middle Ages: Commentaries on Aristotle's Nicomachean Ethics, 1200 -1500<\/em>. Brill.<\/span>",
"deleted": "0",
"directLink": null,
"sortPos": "0000000002",
"googleBooksQuery": 0,
"updated": "2009-01-11",
"elId": "eBUFTSO",
"id": "BUFTSO"
},发布于 2013-12-13 21:57:12
外部内容似乎是一个数组。也许你应该做点什么
file["content"][0]["content"]发布于 2013-12-13 22:11:10
该文件中的JSON结构比散列要复杂得多。我不太清楚什么是正确的参考。FWIW,您的错误是因为
items = file['content']是数组对象。我建议的是探索pry中的结构
gem install pry
pry> require 'json'
pry> foo = JSON.parse(IO.read('test_file.json')
pry> foo.class基本上,JSON对象将是一组嵌套的散列{}或数组[],您需要使用索引或命名值在树下导航。
https://stackoverflow.com/questions/20576612
复制相似问题