需要轉換 JSON 檔案的以下部分,本質上是將其展平為單個哈希sizes
"sizes" : {
"small" : {
"w" : "680",
"h" : "261",
},
"large" : {
"w" : "878",
"h" : "337",
},
"medium" : {
"w" : "878",
"h" : "337",
}
而子屬性可訪問為:
parent['sizes'].each do |size|
w: size['w'],
h: size['h'],
label: size
end
將不起作用,因為將為標簽加載整個哈希。hash的identity string怎么可能只被捕獲呢?
uj5u.com熱心網友回復:
對于散列,為散列each中的每個鍵呼叫一次塊,將鍵值對作為引數傳遞。
像這樣:
parent['sizes'].each do |key, size|
# format your preferred output here
# key is small, large, medium
# and size is {"w"=>"680", "h"=>"261"}, ...
# for example
{
w: size['w'],
h: size['h'],
label: key
}
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/534549.html
標籤:JSON红宝石
