When I deal with values from json, I always get confused because I don't know which of the following two patterns.
hoge [:title]
hoge.title
< Assume that the value of the key is obtained
Handling hash when parsing Json
hoge=resp.body.map { | hoge | JSON.parse(hoge)} OK. hoge[0].title NG hoge [0] ["title" ]
On the other hand, when you put a hash into a variable? is
hoge={:id=>"123",:title=>"test"} OK. hoge ["title" ] NG hoge.title
I thought the behavior was different depending on whether it was included in the variable or not, but
I asked you a question because I didn't come across the literature explaining it in pinpoint.
Also, I sometimes wonder how find behaves in hash.
I would appreciate it if you could let me know if there are any URLs that I can refer to.
Thank you for your cooperation.
ruby
hoge=resp.body.map {|hoge|JSON.parse(hoge)}
I don't know what comes from, so I don't know what's really going on, but the original Hash
has
hash.title
There is no function to specify a key and a value in a method format, as shown in .If possible, it is an extension of Hash
or a completely different object.
By the way, you may have specified your own class for the JSON::Parser.new
argument.
new(source,options=>{})->JSON::Parser [permalink][rdoc]
—object_class
Specifies the class for which the object of the JSON is to be translated.The default is Hash.
—array_class
Specifies the class to convert the array of JSONs.The default is Array.
http://docs.ruby-lang.org/ja/2.1.0/method/JSON=3a=3aParser/s/new.html
© 2024 OneMinuteCode. All rights reserved.