如何使用 Ruby 将这三个 JSON 文件组合成一个文件?

问题描述 投票:0回答:2

我有三个包含类别数据的 JSON 文件。我想把这三者结合起来形成一个结构清晰的单一。

这是第一个 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":[ ] }, {...}

这个是第二个的样子: [ { "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":[ ] }, {...}

这个是第三个: [ { "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

是我最终得到的。目标是三关全过,可是我连前两关都过不了 这是什么原因造成的?

更新

感谢 Jay,我的第一级和第二级都可以正常工作,但我在第三级遇到了麻烦。一些二级类别没有三级类别。但是它们确实是从另一个破坏整个类别结构的类别中添加的。我已经用我使用过的代码更新了问题。似乎是什么问题?

下面是我使用的代码,它返回

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

我做错了什么?

ruby json hash
2个回答
1
投票

您需要跟踪第一个索引以获得与第一个项目关联的第二个项目。
  1. 二级循环太多
  2. 我的代码:

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
.
    


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":[ ] }, {...}

© www.soinside.com 2019 - 2024. All rights reserved.