2014-12-09 10 views
5

Próbujesz przeanalizować niektóre JSON, byt, dlaczego jest text pusty?Ruby - TypeError: no niejawna konwersja Hash na String

Pożądany wyjściowa:

text powinien powrócić Hello world\n\nApple, Harbor\n\nBanana, Kitchen\n\nMango, Bedroom

text = "Hello world" 

json = '{"fruits": [{"name": "Apple", "location": "Harbor"}, {"name": "Banana", "location": "Kitchen"}, {"name": "Mango", "location": "Bedroom"}]}' 
fruits = JSON.parse(json) 

def format_fruits(fruits) 
    fruits.each do |fruit| 
    puts "\n\n#{ fruit[0]['name'] }, #{ fruit[0]['location'] }" 
    end.to_sentence 
end 

text += format_fruits(fruits) 
text 

Obecny wynik:

TypeError: no implicit conversion of Hash into String 
     from (irb):5:in `+' 
     from (irb):5 
     from /home/mark/.gem/ruby/2.1.1/gems/railties-4.1.7/lib/rails/commands/console.rb:90:in `start' 
     from /home/mark/.gem/ruby/2.1.1/gems/railties-4.1.7/lib/rails/commands/console.rb:9:in `start' 
     from /home/mark/.gem/ruby/2.1.1/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:69:in `console' 
     from /home/mark/.gem/ruby/2.1.1/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:40:in `run_command!' 
     from /home/mark/.gem/ruby/2.1.1/gems/railties-4.1.7/lib/rails/commands.rb:17:in `<top (required)>' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `block in require' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require' 
     from /home/mark/myapp/bin/rails:8:in `<top (required)>' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency' 
     from /home/mark/.gem/ruby/2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `run' 
     from /home/mark/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>' 
     from /home/mark/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
     from /home/mark/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
     from -e:1:in `<main>'irb(main):006:0> text 
=> "Hello world" 
irb(main):007:0> 
+1

nie jestem zaznajomiony z 'to_sentence'. Czy możesz to wyjaśnić? – squiguy

+0

'NoMethodError: undefined method to_sentence 'for # ' czy używasz jakiegoś klejnotu? – shivam

+3

'to_sentence' to' ActiveSupport' [metoda] (http://apidock.com/rails/ActiveSupport/CoreExtensions/Array/Conversions/to_sentence) dostępne w Railsach. Ponadto można go wywoływać w Array, a nie w Hash. – kiddorails

Odpowiedz

6

Błąd jest wynikiem nazywając Array#to_sentence na hash.

Oto wersja stała się z komentarzy oznaczający zmiany:

require 'json' 

text = "Hello world" 

json = '{"fruits": [{"name": "Apple", "location": "Harbor"}, {"name": "Banana", "location": "Kitchen"}, {"name": "Mango", "location": "Bedroom"}]}' 
fruits = JSON.parse(json)['fruits'] # append ['fruits'] 

def format_fruits(fruits) 
    fruits.map do |fruit| # change each -> map 
    "\n\n#{ fruit['name'] }, #{ fruit['location'] }" # delete puts, [0] 
    end.join # change to_sentence -> join 
end 

text += format_fruits(fruits) 
puts text 

wyjściowa:

Hello world 

Apple, Harbor 

Banana, Kitchen 

Mango, Bedroom 
+2

Byłoby miło mieć towarzyszące wyjaśnienie, co właściwie było problemem. – JohnDoe

1

W przypadków Twój json zawiera zawsze klucz „owoce”, można to osiągnąć dość łatwo . Tutaj:

fruits = JSON.parse(json) 
#=> {"fruits"=>[{"name"=>"Apple", "location"=>"Harbor"}, {"name"=>"Banana", "location"=>"Kitchen"}, {"name"=>"Mango", "location"=>"Bedroom"}]} 

fruits["fruits"].each { |hash| 
    puts "\n\n#{hash['name']}, #{hash['location']}" 
} 

Innym sposobem, aby to zrobić bez użycia fruits klucz:

fruits.values.first.each { |hash| 
    puts "\n\n#{hash['name']}, #{hash['location']}" 
}