2013-12-13 3 views
0

안녕하세요. 아직 배우고 있어요. 일부 json 데이터 (아래 참조)를 요청하고 각 항목의 날짜를 출력하려고합니다. 그러나 그것은 작동하지 않습니다. 로부터 큰 JSON 파일의 조각 정의되지 않은 메서드 여기 content' for nil:NilClass (NoMethodError)json에서 루비로 값 가져 오기

것 'jsonResponse에 ./philPapers.rb:30:in 블록 : 여기

내가지고있어 첫 번째 오류는

def jsonResponse(response) 

    file = JSON.parse(response) 
    items = file['content']['content'] 
    items.each do |item| 
     puts item['date'] 
    end 
end 

jsonResponse(response) 

을 사용하고 함수의 상단 :

{ 
    "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에서 '블록 : nil을위한'미정 방법 '값'NilClass (NoMethodError) ./philPapers.rb:29에서 \t : 'each '안에 \t ./philPapers.rb:29:in'jsonResponse' \t ./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 객체는 중첩 된 설정됩니다.

관련 문제