我正在尝试使用 ruby 和 open-uri 从谷歌地图网络服务 API 获取丹麦位置地址。
试图得到丹麦艾勒: http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk在 Chrome 中工作,不支持 open-uri:
require 'rubygems'
require "open-uri"
require 'json'
uri = "http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk"
response = open(uri)
array = JSON.parse(response)
pp array
这里它产生
/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk (URI::InvalidURIError)
另一种方法似乎是转义字符:
uri = "http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk"
uri_escaped = URI.escape(uri)
response = open(uri_escaped)
array = JSON.parse(response.read)
pp array
但这会产生转义结果(这并不受欢迎:-)
任何人都知道什么可以解决这个问题(获得未转义的反馈或发送 utf-8 请求)?
这里的Ruby版本是1.8.7
弄清楚了:
只需添加
require 'string19'
到第二个示例的顶部并且它有效
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)