如何使用Ruby将这三个JSON文件合并成一个单独的文件?

lvmkulzt  于 2023-04-29  发布在  Ruby
关注(0)|答案(2)|浏览(251)

我有三个包含类别数据的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

我做错了什么?

1hdlvixo

1hdlvixo1#

如果我理解你想合并这些文件,我认为有几个问题。
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

uidvcgyl

uidvcgyl2#

我想这就是你想要的:

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":[  

            ]
         }, {...}

相关问题