合并两个 json 文件并遍历它们
Combine two json files and iterate over them
我的 Rails 根目录中有两个 json 文件(info.json 和 images.json)。它们都是从我拥有的网络抓取项目中提取的,并且我一直在使用 Mechanize Gem.
所以我有两个 Json 文件,其中一个包含关于植物的信息,如下所示:
info.json
{
"Brazil":[
{"Jungle Plants":[bla bla bla ]},
{"Desert Plants":[ bla bla bla ]}],
"Egypt":[
{"Jungle Plants":[bla bla bla ]},
{"Desert Plants":[ bla bla bla ]}]
and so on...
}
而另一个 Json 文件包含国家/地区标志的图像,如下所示:
images.json
{
"images":
{
"Brazil":"link/to/flag_image.jpg",
"Egypt":"link/to/flag_image.jpg",
and so on...
}
}
我的迁移table:
class CreatePlants < ActiveRecord::Migration[5.2]
def change
create_table :plants do |t|
t.string :country_name
t.jsonb :plant_categories
t.string :flag_photo
t.timestamps
end
end
end
我目前拥有的:
json_file = File.open("#{Rails.root}/info.json").read
json_objects = JSON.parse(json_file).symbolize_keys
json_objects.each { |key, value| Plants.create!(country_name: key, plant_categories: value) }
在我的种子中,如何合并两个 JSON 文件并使它们 pair/match 具有相应的图像标志及其数据并将它们保存在数据库中?
我会感谢你的帮助!
编辑:国旗图片的下载顺序与 info.json 国家信息相同。我想知道如何合并这两个文件并将它们保存在数据库中
以下应该有效:
info = JSON.parse(File.read("#{Rails.root}/info.json"))
images = JSON.parse(File.read("#{Rails.root}/images.json"))
info.each do |country_name, plants|
# Hope the Model name is singular here
plant = Plant.new(country_name: country_name, plant_categories: plants)
path = images['images'][country_name]
if path
# Read the file from remote URL like S3
# images['images'][country_name] - Will return the image URL on the key country_name
attachment = open(images['images'][country_name])
plant.flag_photo = attachment
end
plant.save!
end
我的 Rails 根目录中有两个 json 文件(info.json 和 images.json)。它们都是从我拥有的网络抓取项目中提取的,并且我一直在使用 Mechanize Gem.
所以我有两个 Json 文件,其中一个包含关于植物的信息,如下所示:
info.json
{
"Brazil":[
{"Jungle Plants":[bla bla bla ]},
{"Desert Plants":[ bla bla bla ]}],
"Egypt":[
{"Jungle Plants":[bla bla bla ]},
{"Desert Plants":[ bla bla bla ]}]
and so on...
}
而另一个 Json 文件包含国家/地区标志的图像,如下所示:
images.json
{
"images":
{
"Brazil":"link/to/flag_image.jpg",
"Egypt":"link/to/flag_image.jpg",
and so on...
}
}
我的迁移table:
class CreatePlants < ActiveRecord::Migration[5.2]
def change
create_table :plants do |t|
t.string :country_name
t.jsonb :plant_categories
t.string :flag_photo
t.timestamps
end
end
end
我目前拥有的:
json_file = File.open("#{Rails.root}/info.json").read
json_objects = JSON.parse(json_file).symbolize_keys
json_objects.each { |key, value| Plants.create!(country_name: key, plant_categories: value) }
在我的种子中,如何合并两个 JSON 文件并使它们 pair/match 具有相应的图像标志及其数据并将它们保存在数据库中? 我会感谢你的帮助!
编辑:国旗图片的下载顺序与 info.json 国家信息相同。我想知道如何合并这两个文件并将它们保存在数据库中
以下应该有效:
info = JSON.parse(File.read("#{Rails.root}/info.json"))
images = JSON.parse(File.read("#{Rails.root}/images.json"))
info.each do |country_name, plants|
# Hope the Model name is singular here
plant = Plant.new(country_name: country_name, plant_categories: plants)
path = images['images'][country_name]
if path
# Read the file from remote URL like S3
# images['images'][country_name] - Will return the image URL on the key country_name
attachment = open(images['images'][country_name])
plant.flag_photo = attachment
end
plant.save!
end