我有三个包含类别数据的JSON文件。我想把这三个结合起来,形成一个结构清晰的整体。
This是第一个JSON文件的样子:
{
"filterElementCommands":[
{
"property":"productTaxonomyLevel1.id",
"value":"6401",
"title":"Aardappel, groente, fruit",
"numberOfItems":761,
"numberOfSubFilters":7,
"images":[
{
"width":198,
"height":198,
"url":"#"
}
],
"showGrid":true,
"frozen":false,
"links":[
]
},
{
"property":"productTaxonomyLevel1.id",
"value":"1301",
"title":"Verse kant-en-klaar maaltijden, salades",
"numberOfItems":382,
"numberOfSubFilters":17,
"images":[
{
"width":198,
"height":198,
"url":"#"
}
],
"showGrid":true,
"frozen":false,
"links":[
]
}, {...}
This是第二个的样子:
[
{
"filterElementCommands":[
{
"property":"productTaxonomyLevel2.id",
"value":"1789",
"title":"Aardappelen",
"numberOfItems":87,
"numberOfSubFilters":8,
"images":[
{
"width":200,
"height":200,
"url":"#"
}
],
"showGrid":false,
"frozen":false,
"links":[
]
},
{
"property":"productTaxonomyLevel2.id",
"value":"1628",
"title":"Kruiden, uien, knoflook",
"numberOfItems":39,
"numberOfSubFilters":8,
"images":[
{
"width":200,
"height":200,
"url":"#"
}
],
"showGrid":false,
"frozen":false,
"links":[
]
}, {...}
this是第三个:
[
{
"filterElementCommands":[
{
"property":"productTaxonomyLevel3.id",
"value":"2341",
"title":"Hele aardappel",
"numberOfItems":37,
"numberOfSubFilters":0,
"images":[
],
"showGrid":false,
"frozen":false,
"links":[
]
},
{
"property":"productTaxonomyLevel3.id",
"value":"5243",
"title":"Krieltjes",
"numberOfItems":17,
"numberOfSubFilters":0,
"images":[
],
"showGrid":false,
"frozen":false,
"links":[
]
}, {...}
我想把它们合并成一个单独的一个,格式如下:
{
'property' => 'productTaxonomyLevel1.id',
'value' => '6401',
'title' => 'Aardappel, groente, fruit',
'numberOfItems' => 761,
'numberOfSubFilters' => 7,
'images' => 'http://.../Aardappelen-groente-fruit.png',
'children' => [
{
'property' => 'productTaxonomyLevel2.id',
'value' => '1789',
'title' => 'Aardappelen',
'numberOfItems' => 87,
'numberOfSubFilters' => 8,
'images' => 'http://.../Aaardappelen.jpg',
'children' => [
{
'property' => 'productTaxonomyLevel3.id',
'value' => '2341',
'title' => 'Hele aardappel',
'numberOfItems' => 37,
'numberOfSubFilters' => 0,
'images' => []
},
{...}
]
},
{...}
]
},
{
'property' => 'productTaxonomyLevel1.id',
'value' => '1301',
'title' => 'Verse kant-en-klaar maaltijden, salades',
'numberOfItems' => 382,
'numberOfSubFilters' => 17,
'images' => 'http://.../Shelf-_0001_Kant-en-klaar.png',
'children' => [
{
'property' => 'productTaxonomyLevel2.id',
'value' => '1590',
'title' => 'Stoommaaltijden',
'numberOfItems' => 15,
'numberOfSubFilters' => 0,
'images' => 'http://.../stoommaaltijden.jpg',
'children' => []
}
]
},
{...}
一定是循环出了问题,因为我一直得到相同的第二级类别的所有第一级。
这是我正在使用的代码:
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)
first_from_zero['filterElementCommands'].each do |one|
@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.each_with_index do |two|
two['filterElementCommands'].each_with_index do |three|
@category_2 = {}
@category_2['property'] = three['property']
@category_2['value'] = three['value']
@category_2['title'] = three['title']
@category_2['numberOfItems'] = three['numberOfItems']
@category_2['numberOfSubFilters'] = three['numberOfSubFilters']
@category_2['images'] = three['images']
@category_2['children'] = []
@category['children'] << @category_2
end
end
@master_array << @category
end
File.open("/../combine.json", 'w') do |f|
f.write(@master_array.to_json)
end
end
**this**是我的最终结果。我的目标是通过所有三个关卡,但我连前两个都不能通过。
是什么造成的
更新
多亏了杰伊,我得到了第一和第二个层次的工作,但我有麻烦的第三个层次。一些第二级类别没有第三级类别。但它们确实是从另一个类别添加的,这打破了整个类别结构。我用我使用的代码更新了问题。有什么问题吗?
下面是我使用的代码,它返回**this**。
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)
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_with_index do |two, two_index|
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'] = []
third_from_second[two_index]['filterElementCommands'].each do |three|
category_3 = {}
category_3['property'] = three['property']
category_3['value'] = three['value']
category_3['title'] = three['title']
category_3['numberOfItems'] = three['numberOfItems']
category_3['numberOfSubFilters'] = three['numberOfSubFilters']
category_3['images'] = three['images']
category_2['children'] << category_3
end
category['children'] << category_2
end
master_array << category
end
File.open("../combine.json", 'w') do |f|
f.write(master_array.to_json)
end
end
我做错了什么?
2条答案
按热度按时间1hdlvixo1#
如果我理解你想合并这些文件,我认为有几个问题。
1.您需要跟踪第一个索引以获取与第一个项关联的第二个项。
1.第二层的循环太多。
我的代码:
缩写输出,仅显示标题:
顺便说一下,在你发布的代码中,你可以只使用变量,它们不需要是示例变量。例如,
category
而不是@category
。uidvcgyl2#
我想这就是你想要的: