我正在努力完成一些非常容易的事情,但我无法解决它在我的初学者级别的轨道。问题是如何将一个JSON对象属性分配给rails对象..我正在尝试使用一些参数向Google Map的URL发送请求。没有JSON解析它工作得很好,但是当我尝试解析它时,我收到很多错误。我查询网址时来自Google的常规JSON响应([http://maps.googleapis.com/maps/api/directions/json?origin=40.983204,29.0216549&destination=40.99160908659266,29.02334690093994&sensor=false] [ 1])就像下面一样;
{
"routes" : [
{
"bounds" : {
"southwest" : {
"lat" : 40.98289,
"lng" : 29.02054
},
"northeast" : {
"lat" : 40.99148,
"lng" : 29.02388
}
},
"summary" : "Mühürdar Cd",
"waypoint_order" : [],
"legs" : [
{
"start_location" : {
"lat" : 40.98322,
"lng" : 29.02166
},
"distance" : {
"text" : "1.3 km",
"value" : 1324
},
继续。
我必须获得“summary”属性的代码是;
@request = Net::HTTP.get(URI.parse('http://maps.googleapis.com/maps/api/directions/json?origin=40.983204,29.0216549&destination=40.99160908659266,29.02334690093994&sensor=false'))
@result=JSON.parse(@request)["routes"]["summary"]
我想问一下,从响应中获取摘要属性的正确方法是什么?
答案 0 :(得分:1)
hash['routes'][0]['legs'][0]['duration']['text']
允许您访问文本和
hash['routes'][0]['legs'][0]['duration']['value']
价值。试试吧
hash['routes'][0]['legs'][0]['duration'].class
查看它的类型并相应地访问它