2013-04-14 2 views
1

Ruby에서 일부 JSON 값을 구문 분석하려고하지만 예상치 못한 결과가 계속 발생합니다. count의 값이 0이면Ruby JSON 구문 분석 된 출력 개수

{"count": 0, "items": []} 
2 
#<File:0x00000001a83b48> has SHA256 hash of 3de6c8f12dc4c9efe67c0a5bbfe21502cde5ee22e7ef0bc8d348c696db9a4363 
#<File:0x00000001a83238> has SHA256 hash of 3de6c8f12dc4c9efe67c0a5bbfe21502cde5ee22e7ef0bc8d348c696db9a4363 

, 왜 나에게 2의 카운트 값을 (inputfile의 단지 지역의 예제 파일입니다)주고있다 :

articlelist = client.get('/v1/my_data/articles') 
#debug 
puts (articlelist.body) 

#Parse the article list and get values 
parsed = JSON.parse(articlelist.body) 
puts parsed.count 

parsed.count do 
    |currentfile| 
    inputfile = File.open ('file.example')#file.example should be replaced with local file 
    filehash = OpenSSL::Digest.new('sha256', 'inputfile') 
    puts "#{inputfile} has #{filehash.name} hash of #{filehash}"#debug 
end 

나는 다음과 같은 결과를 얻을?

답변

1

parsed은 두 요소가있는 해시이므로 parsed.count2입니다. 한편, parsed['count']0입니다.

+0

그래서'count'는 일반적인 루비 객체의 메소드입니까? 아마 그걸 알고 있었어 야 했어. 감사! – user2276204

+1

해시 및 배열과 같은 컬렉션 개체가 있습니다. 나는 당신이 JavaScript로부터 왔다고 추측한다.'parsed.count'와'parsed [count] '는 같은 것을 의미한다. Ruby에서는 해시 요소에 후자의 형식으로 만 액세스 할 수 있으며 이전 양식은 개체에 대한 메서드를 호출합니다. –