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 液化天然气,图标

& 抱歉,我使用图片只是为了在终端 image 中显示我的实际响应

最佳答案

您可以通过执行 res.class 来检查您的 res 对象类型是什么;如果它还不是一个散列,你可以按照步骤 here将其转换为散列。

使用 res 哈希,您可以像这样返回 JSON 数组 -

[res["place_id"], res["name"], res["lat"], res["lng"], res["icon"]]

https://stackoverflow.com/questions/63791479/

相关文章:

node.js - Next.js - GetStaticPaths 类型错误

git - 通过 git push 模拟 git pull --rebase

javascript - Three.JS - 使用相同的骨骼结构在 SkinnedMeshes 之

amazon-web-services - 如何更改 CodePipeline 工件行为? (如果可

scala - 如何解决 `/packages cannot be represented as U

java - Apache Storm 本地集群 "Unable to canonicalize a

android - 运行错误 01 46,响应 : . ..UNABLETOCONNECT

asp.net-core - 如何访问 Program.cs Main 方法中的 IHostingE

python-3.x - 由于使用 Docker 的 PostgresDB 连接失败,无法启动 Ap

typescript - 键入一个 "type predicate"没有任何