ruby-on-rails - 从哈希数组返回特定值 - JSON
问题描述
嗨,我正在学习 rails,在我的应用程序中,我从 json 格式中获取值
res = @client.spots(params[:location][:lat], params[:location][:lng], :types => ['restaurant','food'], :radius => 500)
在我的 res 对象中,我得到了这些值
{
"json_result_object": {
"business_status": "OPERATIONAL",
"geometry": {
"location": {
"lat": 31.4734563,
"lng": 74.2794873
},
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"name": "CP Five Star",
"opening_hours": {
"open_now": true
}
}
],
"place_id": "ChIJjfE-TdoDGTkRMom8KQfI9Uc",
"plus_code": {
"compound_code": "F7FH+9Q Lahore, Pakistan",
"global_code": "8J3PF7FH+9Q"
},
},
"reference": "ChIJjfE-TdoDGTkRMom8KQfI9Uc",
"place_id": "ChIJjfE-TdoDGTkRMom8KQfI9Uc",
"vicinity": "Bilal Arcade, Abdul Haque Road, Block G Block G Phase 1 Johar Town, Lahore",
"lat": 31.4734563,
"lng": 74.2794873,
"viewport": {
"northeast": {
"lat": 31.4748154802915,
"lng": 74.28081473029151
},
"southwest": {
"lat": 31.4721175197085,
"lng": 74.2781167697085
}
},
"name": "CP Five Star",
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"types": [
"restaurant",
"food",
"point_of_interest",
"establishment
}
现在我只想返回一个 JSON 数组,其中仅包含以下值 place_id 、 name 、 lat lng 、 icon
&对不起,我使用图像只是为了在终端图像中显示我的实际响应
解决方案
您可以通过以下方式检查您的 res 对象类型res.class
;如果它还不是哈希,您可以按照此处的步骤将其转换为哈希。
使用 res 哈希,您可以像这样返回 JSON 数组 -
[res["place_id"], res["name"], res["lat"], res["lng"], res["icon"]]
推荐阅读
- r - 在 R 中的 searchTwitter 包中使用过滤器
- excel - 从 Excel 调用 VBA 函数,“查找”故障?
- java - 在android studio上读取Json文件的简单方法
- docker - 重启服务后 Docker Container 消失
- php - 从扩展 Doctrine\DBAL\Connection 的类中的第二个数据库连接中获取数据
- android - 如何修复 ArrayAdapter 中的类型不匹配?
- flutter - Flutter 垂直可滚动抽屉
- docker - 如何在 chrome 中调试无头 pdf 打印问题?
- .net-core - .net core 短生命周期缓解
- javascript - 如何识别网页中是否存在滚动。还得到它的坐标吗?