Lorem存有是印刷和排版的只是一个虚拟的文本。 Lorem存有一直是标准的虚拟文本,自从16世纪,当一个未知的打印机采取了与字母组成棒和炒它做的字体目录。它已存活不仅五个世纪,但也几乎没有变化成电子排版。它是在20世纪60年代开始流行用含Lorem存有通道Letraset张的释放,以及最近与桌面排版软件,如奥尔德斯PageMaker中包括Lorem存有版本。如何合并论文3个JSON文件合并成一个单一的一个使用Ruby?
0
A
回答
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"
},
...
]
},
...
]
一点题外话,在您发布的代码,你可以使用变量,他们不' t需要成为实例变量。例如,category
而非@category
。
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. 将多个JSON文件合并到一个Ruby文件
- 2. 合并多个PDF文件合并成一个用PHP Ghostscript的
- 3. 合并多个的.csv文件合并成一个
- 4. 如何合并多个日志文件合并成一个.LDF文件在SQL2000
- 5. 多个文件合并成一个单一的数据帧
- 6. 猛砸合并圆柱文件合并成一个文件,行
- 7. 合并多个CSV文件合并成一个excel表
- 8. 合并文件在一个
- 9. 基于第一列合并两个文件合并为一个
- 10. 将Flash SWF文件合并成一个
- 11. 将3个表合并成一个表
- 12. FFmpeg的:Falied两个MPEG TS文件合并成一个单一的MP4文件
- 13. 我想将三个txt文件合并成一个单一的文件在c#
- 14. 如何在一个表中合并3个文件?
- 15. 如何将多个PDF文件(或PNG)合并成一个单一的页面
- 16. 合并/合并多行成一行从一个文本文件(PowerShell的)
- 17. 如何将两个wav文件合并/合并为一个wav文件?
- 18. 合并文件合并成一个在XP
- 19. 批合并多个文件到一个
- 20. 多个JavaScript文件合并为一个
- 21. 合并多个数据文件,以一个单一的图形
- 22. 将多个文件流合并成一个文件?
- 23. 合并三个文件分割成一个大文件
- 24. 匹配行和猫多个文件合并成一个文件
- 25. 将多个JavaScript文件合并成一个JS文件
- 26. 将两个文件pdf合并成一个文件覆盖?
- 27. 你可以将多个文件合并成一个文件吗?
- 28. 如何将2个XLSX文件合并为一个使用NodeJS
- 29. 使用glob将多个xml文件合并为一个文件
- 30. 合并两个音频文件合并为一个目标C
我遇到一些麻烦与第三级。一些二级类别没有三级类别。但他们得到来自其他类别的众多追随者如何打破整个类别结构中增加。我用我用过的代码更新了这个问题。什么似乎是问题? – narzero 2014-08-28 11:17:45
我错了'需要第三级two_index'。远眺third_from_second的数据,你只需要保持跟踪你在该文件是什么型号。 – 2014-08-28 15:25:34
编辑的代码大约显示第三级别的代码是什么样子。 – 2014-08-28 15:30:06