【发布时间】:2019-12-27 17:10:51
【问题描述】:
我有以下 JSON:
{"connection": {
"informations": {},
"id_user": 6909,
"subscriptions": [
{
"id_source": 69825,
"documents": [
{
"id_subscription": 5265,
"thumb_url": "https://test-sandbox.biapi.pro/2.0/users/me/documents/1244/thumbnail/test%40hotmail.fr_305146953_thumbnail.png",
"name": "Facture de 44.96€ du 2019-11-18",
"id_thumbnail": 40343,
"url": "https://test-sandbox.biapi.pro/2.0/users/me/documents/1244/file/test%40hotmail.fr_305146953.pdf",
"untaxed_amount": null
},
{
"id_subscription": 5265,
"thumb_url": "https://mooncard23-sandbox.biapi.pro/2.0/users/me/documents/1245/thumbnail/test%40hotmail.fr_290804394_thumbnail.png",
"name": "Facture de 13.98€ du 2019-05-29",
"url": "https://mooncard23-sandbox.biapi.pro/2.0/users/me/documents/1245/file/test%40hotmail.fr_290804394.pdf",
"untaxed_amount": null
}
]
}
]
}
}
我尝试获取所有值,例如“https://test-sandbox.biapi.pro/2.0/users/me/documents/1246/file/test%40hotmail.fr_274475652.pdf”
我的 JSON 在我的控制器中呈现如下:
render json: params.to_json
当我尝试以这种方式获取所有值 URL 时:
params.dig('connection','subscriptions').each_with_object([]) { |x, arr| x['documents'].each { |d| arr << d['url'] } }
我有错误undefined method each_with_object for nil:NilClass。
【问题讨论】:
-
欢迎来到 SO!寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。请参阅:“How to create a Minimal, Reproducible Example”。当您未能将代码或数据减少到最低限度时,您将浪费您自己和我们的时间,因为您通过向我们抛出不必要的信息,使我们更难帮助您。尽量减少噪音是您的工作,这样我们才能更有效地帮助您。
-
编辑问题,减少代码和数据并解释“它不起作用”是什么意思:你得到错误,你什么都得不到,你得到错误的结果吗?帮助我们帮助您。