Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails呈现从json响应中提取值_Ruby On Rails_Json_Typhoeus - Fatal编程技术网

Ruby on rails rails呈现从json响应中提取值

Ruby on rails rails呈现从json响应中提取值,ruby-on-rails,json,typhoeus,Ruby On Rails,Json,Typhoeus,我有一个Rails应用程序,我正在尝试从解析的JSON散列中呈现一个项目数组 我当前的render语句如下所示 resp = JSON.parse(response.body) render json: resp 我正在使用Typheous,但此代码对我无效: resp = JSON.parse(response.body).fetch("item") 以下是JSON散列(项键有许多值,但为了简洁起见,我只显示一个值): 如何从解析的JSON散列中呈现项目数组?由于ebay和FindItem

我有一个Rails应用程序,我正在尝试从解析的JSON散列中呈现一个项目数组

我当前的render语句如下所示

resp = JSON.parse(response.body)
render json: resp
我正在使用Typheous,但此代码对我无效:

resp = JSON.parse(response.body).fetch("item")
以下是JSON散列(项键有许多值,但为了简洁起见,我只显示一个值):


如何从解析的JSON散列中呈现项目数组?

由于
ebay
FindItemsByWordsResponse
键只有一个值(根据OP的注释),您可以通过如下操作检索
项目数组:

resp = JSON.parse(response.body)
resp[:ebay].first[:findItemsByKeywordsResponse].first[:searchResult].first[:item]
这将为您提供一个包含
itemId
和任何其他键值对的哈希数组


您希望包含
.first
(或
[0]
)的原因是,基于解析的JSON响应,您的哈希包含一个一直嵌套到
数组的哈希数组。如果存在多个
searchResult
值,则在获取
数组之前,您需要迭代这些值。

对于
findItemsByWordsResponse
searchResult
键,是否有许多值?如果是这样的话,您必须遍历这些键中的每一个,以缩小到
数组。例如,使用发布的散列,为了获得单个
项目的数组
,您需要如下内容:
resp[:ebay].first[:findItemsByKeywordsResponse].first[:searchResult].first[:item]
,它将生成
{:itemId=>[“321453454731”}
否易趣和FindItemsKeywords只有一个值响应我需要完整的物品列表/ArrayTanks解释很有帮助。这对我来说是有效的呈现json:resp[“FindItemsByWordsResponse”]。first[“searchResult”]。first[“item”]和呈现json:resp.first[“FindItemsByWordsResponse”]。first[“searchResult”]。first[“item”]目前不起作用,不知道为什么欢迎您。如果
resp.first…
不起作用,则表示
resp
中的
first
对象没有
findItemsByWordsResponse
键。您可以
检查
或打印解析后的JSON响应,以查看其完整结构以及响应的键。
resp = JSON.parse(response.body)
resp[:ebay].first[:findItemsByKeywordsResponse].first[:searchResult].first[:item]