我有一个Rails应用程序,我试图从解析的JSON哈希渲染项目数组。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”]}' – mmichael
不,只有一个eBay价值和findItemsKeywordsResponse我需要整个项目列表/数组 – Rigel