【发布时间】:2014-07-10 17:38:13
【问题描述】:
我有一个 Rails 应用程序,我正在尝试从解析的 JSON 哈希中呈现一组项目。
我当前的渲染语句如下所示
resp = JSON.parse(response.body)
render json: resp
我正在使用 Typheous,但这段代码对我不起作用:
resp = JSON.parse(response.body).fetch("item")
以下是 JSON 哈希(item 键有很多值,但为了简洁我只显示一个):
{
ebay: [{
findItemsByKeywordsResponse: [{
ack: [],
version: [],
timestamp: [],
searchResult: [{
count: "91",
item: [{
itemId: [ "321453454731" ]
}]
}]
}]
}]
}
如何从解析的 JSON 哈希中呈现一组项目?
【问题讨论】:
-
findItemsByKeywordsResponse和searchResult键是否有多个值?如果是这样,您将不得不遍历每个键以缩小到item数组。例如,使用发布的哈希,为了获得单个item的数组,你需要这样的东西:resp[:ebay].first[:findItemsByKeywordsResponse].first[:searchResult].first[:item],这将产生{:itemId=>["321453454731"]} -
不,ebay 和 findItemsKeywordsResponse 只有一个值,我需要整个项目列表/数组
标签: ruby-on-rails json typhoeus