【发布时间】:2016-09-02 20:10:06
【问题描述】:
我正在使用 Google Civic Information API。它给了我两个哈希数组。我想在屏幕上打印两者的信息。哈希 1 有一些整数作为 k-v 对(官方索引)。这些表示第二个哈希中相应对象的索引号。这两个怎么合并?我想同时显示来自两个哈希的信息。也许用第二个数组中的索引哈希替换 officialIndices 的值会更好。感谢您的任何建议!
哈希 1:
{
"name" => "President of the United States",
"divisionId" => "ocd-division/country:us",
"levels" => ["country"],
"roles" => ["headOfState", "headOfGovernment"],
"officialIndices" => [0]
}
哈希 2:
{
"name" => "Barack Obama",
"address" => [{
"line1" => "The White House",
"line2" => "1600 pennsylvania avenue nw",
"city" => "washington",
"state" => "DC",
"zip" => "20500"
}],
"party" => "Democratic",
"phones" => ["(202) 456-1111"],
"urls" => ["http://www.whitehouse.gov/"],
"photoUrl" => "http://www.whitehouse.gov/sites/default/files/imagecache/admin_official_lowres/administration-official/ao_image/president_official_portrait_hires.jpg",
"channels" => [
{ "type" => "GooglePlus", "id" => "+whitehouse" },
{ "type" => "Facebook", "id" => "whitehouse" },
{ "type" => "Twitter", "id" => "whitehouse" },
{ "type" => "YouTube", "id" => "barackobama" }
]
}
编辑**澄清一下,哈希 1 是哈希数组中的第一个哈希。哈希 2 是哈希数组中的第一个哈希。我想用 Hash 2 替换 Hash 1 中的 officialIndice 中的数字。这让我很困惑,因为一些 officialIndice 有多个数字。希望这是有道理的。
【问题讨论】:
-
你说,“这些代表第二个哈希中对应对象的索引号。”什么是“对应对象”?该索引如何用于构建合并的哈希?请编辑以澄清并显示您的示例所需的合并哈希。
标签: ruby-on-rails ruby api hash