0

画像モデルには、id、file_name、および説明が含まれています。Gem: RABL または GRAPE のいずれか

通常の出力は次のとおりです。

{
    "images": [
        {
            "id": 48660,
            "file_name": "9e0f6.jpg",
            "description": "View 1"
        },
        {
            "id": 48665,
            "file_name": "fd42f.jpg",
            "description": "View 2"
        },
        {
            "id": 48662,
            "file_name": "477e8.jpg",
            "description": "View 3"
        }
    ]
}

次のように、属性/キーを削除して値を配列に変換するにはどうすればよいですか?

{
    "images":[
        [
            48660,
            "9e0f6.jpg",
            "View 1"
        ],
        [
            48665,
            "fd42f.jpg",
            "View 2"
        ],
        [
            48662,
            "477e8.jpg",
            "View 3"
        ]
    ]
}
4

1 に答える 1

0

を使用してハッシュに変換できます

JSON.parse(json)

キーなしでjsonを再構築します

json_hash = JSON.parse(json)

inner_array = []
json_hash["images"].each do |elem|
  inner_array << elem.collect{|key, value| value unless ["file_name"].include?(key)}
end

json_hash["images"] = inner_array
json_hash.to_json
于 2013-06-11T15:35:30.817 に答える