【问题标题】:Combine 2 Arrays with a single Dictionary将 2 个数组与单个字典组合
【发布时间】:2015-07-26 17:39:15
【问题描述】:

我目前正在获取两个 JSON 数组,并将它们存储在名为 json_one 和 json_two 的变量中。一切正常,但现在我想以某种方式将它们组合成一个数组。

如何将 2 个数组与一个字典结合起来?

json_one

[
  {
    "address": "1031 25th Street San Diego, CA 92102",
    "id": 1,
    "resource_type_id": 3,
  }
]

json_two

 [
   {
     "resource_id": 4,
   }
 [

我试过这个......

def show
    @user = User.find(params[:user_id])
    @favorite = @user.favorites.find(params[:id])
      @resource_data = []
      (@resource_data << @favorite.resource_type.community_resources.where(:id => @favorite.resource_id)).flatten!
      (@resource_data << @favorite.resource_type.district_resources.where(:id => @favorite.resource_id)).flatten!
      (@resource_data << @favorite.resource_type.military_resources.where(:id => @favorite.resource_id)).flatten!
      (@resource_data << @favorite.resource_type.school_resources.where(:id => @favorite.resource_id)).flatten!

      @favorite_data = []
      (@favorite_data << @favorite)

      json_one = @resource_data.to_json(:only => [:id, :resource_type_id,:district_id,
        :school_id, :name, :description, :website, :phone, :email, :address, :latitude, :longtude]) 

      json_two = @favorite_data.to_json(:only => [:resource_id])

      array1 = JSON.parse(json_one)
      array2 = JSON.parse(json_two)

      #array1 << array2
      #firsts = array2.map {|array2| array2.first}

      combined = [ array1.first.merge(array2.first) ]

      respond_to do |format|
        format.html
        format.json { render json: data }
      end
  end

###but this is my result. 
###I would like both dictionaries to be combined into one.

[
  {
    "address": "1031 25th Street San Diego, CA 92102",
    "id": 1,
    "resource_type_id": 3,
  }
  {
    "resource_id": 4,
  }
]

【问题讨论】:

    标签: ruby-on-rails arrays ruby json dictionary


    【解决方案1】:
    json_one = "[{\"address\":\"1031 25th Street San Diego, CA 92102\",\"id\":1,\"resource_type_id\":3}]"
    json_two = "[{\"resource_id\":4}]"
    
    array1 = JSON.parse(json_one)
    array2 = JSON.parse(json_two)
    
    combined = [ array1.first.merge(array2.first) ]
    
    respond_to do |format|
      format.html
      format.json { render json:combined }
    end
    

    FWIW,我会在没有 JSON 的情况下这样做:

    combined = @resource_data.slice( :id, :resource_type_id,:district_id,
                                     :school_id, :name, :description, :website, 
                                     :phone, :email, :address, :latitude, :longtude ) 
                             .merge( @favorite_data.slice( :resource_id ))
    

    这会从@resource_data 中提取所需的属性,并将其与@favorite_data resource_id 属性合并。

    Rails slice docs

    【讨论】:

    • 你需要解析 JSON。 array_1 和 array_2 从 JSON 中解析出来:array1 = JSON.parse(json_one)
    • 不确定该错误来自何处。你能把json_onejson_two放进去吗?
    • 我认为您不需要 JSON。 @resource_data@favorite 是什么?
    • 我已经更新了上面的代码,收藏夹是收藏夹的ID,resource_data取决于它在哪里找到我需要的数据。但它始终是该资源的一本字典。
    • 不客气。我用更好的方法更新了答案。
    猜你喜欢
    • 2021-12-07
    • 2012-09-20
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-04-19
    相关资源
    最近更新 更多