gpt4 book ai didi

ruby-on-rails - 从 JSON 嵌套哈希中提取特定字段

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:45 24 4
gpt4 key购买 nike

我正在考虑编写一个网络应用程序来抓取 API 并以 JSON 形式返回此信息。

但是,我只关注一个数字,然后是当前价格(在本示例中为“227”)。我如何在 Ruby 中访问它?我不知道从哪里开始。我从来没有处理过这样的文本。

为了便于讨论,假设我将此输出保存到实例变量 @information

{
"item": {
"icon": "http://services.runescape.com/m=itemdb_rs/4332_obj_sprite.gif?id=4798",
"icon_large": "http://services.runescape.com/m=itemdb_rs/4332_obj_big.gif?id=4798",
"id": 4798,
"type": "Ammo",
"typeIcon": "http://www.runescape.com/img/categories/Ammo",
"name": "Adamant brutal",
"description": "Blunt adamantite arrow...ouch",
"current": {
"trend": "neutral",
"price": 227
},
"today": {
"trend": "neutral",
"price": 0
},
"day30": {
"trend": "positive",
"change": "+1.0%"
},
"day90": {
"trend": "positive",
"change": "+1.0%"
},
"day180": {
"trend": "positive",
"change": "+2.0%"
},
"members": "true"
}
}

最佳答案

首先按照这个帖子将这个JSON解析成Hash Parsing a JSON string in Ruby

假设解析的哈希名称是 my_hash 那么下面应该给你价格

my_hash['item']['current']['price']

编辑:

正如你所说,你想把它保存在@information

@information = my_hash['item']['current']['price']

关于ruby-on-rails - 从 JSON 嵌套哈希中提取特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com