如何在另一个json对象中总结/恢复json对象

  • 本文关键字:json 对象 恢复 另一个 ruby
  • 更新时间 :
  • 英文 :


如何将一个json数据汇总到另一个json数据中,也就是说,将所有的key:value​​在另一个json中,而无需再次定义。

例如:

default_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Content-Type': 'application/json'
}
special_headers = { default_headers, 'Authorization': 'Bearer xxxxxxx'}
# special_headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
#    'Content-Type': 'application/json', 'Authorization': 'Bearer xxxxxxx' }

您可以将新的散列Hash#merge转换为现有的散列:

{
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Content-Type': 'application/json'
}.merge('Authorization': 'Bearer xxxxxxx')
# {:"User-Agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36", :"Content-Type"=>"application/json", :Authorization=>"Bearer xxxxxxx"}

最新更新