加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

无法使用点语法进行ruby哈希

发布时间:2020-12-16 20:45:36 所属栏目:百科 来源:网络整理
导读:我使用net / http从Yahoo Placemaker API中提取一些json数据.收到回复后,我在响应中执行JSON.parse.这给我一个哈希,看起来像: {"processingTime"="0.001493","version"="1.4.0.526 build 222223","documentLength"="25","document"={"administrativeScope"=
我使用net / http从Yahoo Placemaker API中提取一些json数据.收到回复后,我在响应中执行JSON.parse.这给我一个哈希,看起来像:
{"processingTime"=>"0.001493","version"=>"1.4.0.526 build 222223","documentLength"=>"25","document"=>{"administrativeScope"=>{"woeId"=>"2503863","type"=>"Town","name"=>"Tampa,FL,US","centroid"=>{"latitude"=>"27.9465","longitude"=>"-82.4593"}},"geographicScope"=>{"woeId"=>"2503863","localScopes"=>{"localScope"=>{"woeId"=>"2503863",US (Town)","longitude"=>"-82.4593"},"southWest"=>{"latitude"=>"27.8132","longitude"=>"-82.6489"},"northEast"=>{"latitude"=>"28.1714","longitude"=>"-82.2539"},"ancestors"=>[{"ancestor"=>{"woeId"=>"12587831","type"=>"County","name"=>"Hillsborough"}},{"ancestor"=>{"woeId"=>"2347568","type"=>"State","name"=>"Florida"}},{"ancestor"=>{"woeId"=>"23424977","type"=>"Country","name"=>"United States"}}]}},"extents"=>{"center"=>{"latitude"=>"27.9465","longitude"=>"-82.2539"}},"placeDetails"=>{"placeId"=>"1","place"=>{"woeId"=>"2503863","placeReferenceIds"=>"1","matchType"=>"0","weight"=>"1","confidence"=>"8"},"referenceList"=>{"reference"=>{"woeIds"=>"2503863","placeReferenceId"=>"1","placeIds"=>"1","start"=>"15","end"=>"20","isPlaintextMarker"=>"1","text"=>"Tampa","type"=>"plaintext","xpath"=>""}}}}

我可以通过像jsonResponse [‘version’]这样的东西来访问元素,但是我无法做jsonResponse.version.为什么是这样?

解决方法

哈希没有点语法的键. OpenStruct确实:
require 'ostruct'
hash = {:name => 'John'}
os = OpenStruct.new(hash)
p os.name #=> "John"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读