Lorem Ipsum是一个小小的专业人士,他们认为他们是个好主意。 Lorem Ipsum是deze bedrijfstak sinds de 16e eeuw的de standaard proeftekst,toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken。 Het heeft niet alleen vijf eeuwen overleefd maar is ook,vrijwel onveranderd,overgenomen in elektronische letterzetting。 Het is de jaren '60 populair geworden met de introductionctie van Letraset vellen met Lorem Ipsum passages en meer recentelijk door桌面出版软件zoals Aldus PageMaker die versies van Lorem Ipsum bevatten。
答案 0 :(得分:1)
我认为这可以满足您的需求:
files = [
File.open('json1.txt'),
File.open('json2.txt'),
File.open('json3.txt'),
]
File.open('combined_json.txt', 'w') do |f|
while (len = files.length) > 0
index = rand(len)
if line = files[index].gets
f.puts line
else
files.delete_at(index)
end
end
end
files.each do |f|
f.close
end
--output:--
[
{
{
"filterElementCommands":[
{
"filterElementCommands":[
{
"property":"productTaxonomyLevel1.id",
"value":"6401",
"property":"productTaxonomyLevel3.id",
"value":"2341",
"title":"Aardappel, groente, fruit",
[
"numberOfItems":761,
{
"numberOfSubFilters":7,
"images":[
"filterElementCommands":[
{
"title":"Hele aardappel",
"width":198,
"height":198,
{
"property":"productTaxonomyLevel2.id",
"value":"1789",
"title":"Aardappelen",
"url":"#"
}
"numberOfItems":37,
"numberOfSubFilters":0,
"numberOfItems":87,
"numberOfSubFilters":8,
"images":[
"images":[
],
"showGrid":true,
{
"width":200,
"height":200,
],
"showGrid":false,
"frozen":false,
"url":"#"
"links":[
"frozen":false,
}
]
"links":[
},
{
"property":"productTaxonomyLevel3.id",
],
"showGrid":false,
"frozen":false,
]
"value":"5243",
"title":"Krieltjes",
},
"numberOfItems":17,
"links":[
{
"numberOfSubFilters":0,
"property":"productTaxonomyLevel1.id",
"value":"1301",
"images":[
"title":"Verse kant-en-klaar maaltijden, salades",
"numberOfItems":382,
"numberOfSubFilters":17,
"images":[
]
],
},
{
"property":"productTaxonomyLevel2.id",
"showGrid":false,
{
"value":"1628",
"title":"Kruiden, uien, knoflook",
"frozen":false,
"width":198,
"links":[
"height":198,
"url":"#"
"numberOfItems":39,
}
],
"numberOfSubFilters":8,
]
"showGrid":true,
"frozen":false,
"links":[
}, {...}
]
}, {...}
"images":[
{
"width":200,
"height":200,
"url":"#"
}
],
"showGrid":false,
"frozen":false,
"links":[
]
}, {...}
答案 1 :(得分:1)
如果我理解你希望如何合并这些文件,我认为存在一些问题。
我的代码:
require 'json'
def combine_all_categories(level = 1, args = {:invoker => :@category})
@master_array = []
first_from_zero = JSON.parse(IO.read('first_from_zero.json').scrub)
second_from_first = JSON.parse(IO.read('second_from_first.json').scrub)
third_from_second = JSON.parse(IO.read('third_from_second.json').scrub)
third_index = 0
first_from_zero['filterElementCommands'].each_with_index do |one, one_index|
@category = {}
@category['property'] = one['property']
@category['value'] = one['value']
@category['title'] = one['title']
@category['numberOfItems'] = one['numberOfItems']
@category['numberOfSubFilters'] = one['numberOfSubFilters']
@category['images'] = one['images']
@category['children'] = []
second_from_first[one_index]['filterElementCommands'].each do |two|
@category_2 = {}
@category_2['property'] = two['property']
@category_2['value'] = two['value']
@category_2['title'] = two['title']
@category_2['numberOfItems'] = two['numberOfItems']
@category_2['numberOfSubFilters'] = two['numberOfSubFilters']
@category_2['images'] = two['images']
@category_2['children'] = []
# Use third_index to retrieve the item from the third file.
third_from_second[third_index]['filterElementCommands'].each do |third|
# Build children....
end
third_index += 1
@category['children'] << @category_2
end
@master_array << @category
end
File.open("combine.json", 'w') do |f|
f.write(@master_array.to_json)
end
end
缩写输出,只显示标题:
[
{
"title": "Aardappel, groente, fruit",
"children": [
{
"title": "Aardappelen"
},
...
]
},
{
"title": "Verse kant-en-klaar maaltijden, salades",
"children": [
{
"title": "Stoommaaltijden"
},
...
]
},
...
]
作为旁注,在您发布的代码中,您可以只使用变量,它们不需要是实例变量。例如,category
而不是@category
。