Hi I'm still learning and I need some help.I making a request for some json data (see below) and I'm trying to print out the date for each item. But its not working.
嗨,我还在学习,我需要一些帮助。我请求一些json数据(见下文),我正在尝试打印出每个项目的日期。但它不起作用。
Here's the function i'm using
这是我正在使用的功能
def jsonResponse(response)
file = JSON.parse(response)
items = file['content']['content']
items.each do |item|
puts item['date']
end
end
jsonResponse(response)
The first error I'm getting is: ./philPapers.rb:30:in
block in jsonResponse': undefined method content' for nil:NilClass (NoMethodError)
我得到的第一个错误是:./philPapers.rb:30:inblock in jsonResponse':undefined method content'for nil:NilClass(NoMethodError)
Here's a snippet of a large json file from the top:
这是从顶部开始的一个大型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://rads.*.com/amzn/click/9004163166">$132.19 new<\/a><\/span> <span class='price_amazon'><a class='price_amazon' target=\"_blank\" rel=\"nofollow\" href=\"http://rads.*.com/amzn/click/9004163166">$149.00 direct from Amazon<\/a><\/span> <span class='price_used'><a class='price_used' target=\"_blank\" rel=\"nofollow\" href=\"http://rads.*.com/amzn/click/9004163166">$203.83 used<\/a><\/span> (collection) <a href=\"http://rads.*.com/amzn/click/9004163166">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"
},
2 个解决方案
#1
0
The outer content seems like an array. Maybe you should be doing something like
外部内容看起来像一个数组。也许你应该做的事情
file["content"][0]["content"]
#2
0
The JSON structure in that file is much more complicated than a hash of hashes. It's not exactly obvious to me what the right reference should be. FWIW, your error is caused because
该文件中的JSON结构比散列哈希要复杂得多。对我来说,正确的参考应该是什么并不是很明显。 FWIW,你的错误是因为
items = file['content']
is an Array object. What I would suggest is exploring the structure in pry
是一个Array对象。我建议的是探索撬的结构
gem install pry
pry> require 'json'
pry> foo = JSON.parse(IO.read('test_file.json')
pry> foo.class
Basically, JSON objects will be a nested set of either hashes {}
or arrays []
and you'll need to navigate down the tree using either indexes or named values.
基本上,JSON对象将是哈希{}或数组[]的嵌套集合,您需要使用索引或命名值向下导航树。
#1
0
The outer content seems like an array. Maybe you should be doing something like
外部内容看起来像一个数组。也许你应该做的事情
file["content"][0]["content"]
#2
0
The JSON structure in that file is much more complicated than a hash of hashes. It's not exactly obvious to me what the right reference should be. FWIW, your error is caused because
该文件中的JSON结构比散列哈希要复杂得多。对我来说,正确的参考应该是什么并不是很明显。 FWIW,你的错误是因为
items = file['content']
is an Array object. What I would suggest is exploring the structure in pry
是一个Array对象。我建议的是探索撬的结构
gem install pry
pry> require 'json'
pry> foo = JSON.parse(IO.read('test_file.json')
pry> foo.class
Basically, JSON objects will be a nested set of either hashes {}
or arrays []
and you'll need to navigate down the tree using either indexes or named values.
基本上,JSON对象将是哈希{}或数组[]的嵌套集合,您需要使用索引或命名值向下导航树。