2013-12-13 43 views
0

嗨,我還在學習,我需要一些help.I請求一些json數據(見下文),我試圖打印出每個項目的日期。但它不工作。從json獲取ruby值

下面是我使用

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://rads.stackoverflow.com/amzn/click/9004163166">$132.19 new<\/a><\/span>&nbsp;&nbsp;&nbsp;<span class='price_amazon'><a class='price_amazon' target=\"_blank\" rel=\"nofollow\" href=\"http://rads.stackoverflow.com/amzn/click/9004163166">$149.00 direct from Amazon<\/a><\/span>&nbsp;&nbsp;&nbsp;<span class='price_used'><a class='price_used' target=\"_blank\" rel=\"nofollow\" href=\"http://rads.stackoverflow.com/amzn/click/9004163166">$203.83 used<\/a><\/span>&nbsp;&nbsp;&nbsp;(collection)&nbsp;&nbsp;&nbsp;<a href=\"http://rads.stackoverflow.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" 
       }, 
+0

我們展示完整的錯誤.... –

+0

/philPapers.rb:30:in在jsonResponse'塊 ':未定義的方法'值' 的零:NilClass(NoMethodError) \t從./philPapers.rb:29:在'每個' \t from ./philPapers.rb:29:in'jsonResponse' \t from ./philPapers.rb:34:in'

' – Jeff

回答

0

外部內容看起來像一個數組。也許你應該做類似

file["content"][0]["content"] 
+0

我想就是這樣。謝謝。 JW – Jeff

0

該文件中的JSON結構比散列哈希複雜得多。這對我來說並不明顯,正確的參考應該是什麼。 FWIW,您的錯誤是由於

items = file['content'] 

是一個Array對象。我的建議是探索pry

gem install pry 

pry> require 'json' 
pry> foo = JSON.parse(IO.read('test_file.json') 
pry> foo.class 

基本結構,JSON對象將是一組嵌套的任何散列{}或陣列[],你需要導航下使用任何索引或命名值樹。