2014-08-27 64 views
0

Lorem存有是印刷和排版的只是一個虛擬的文本。 Lorem存有一直是標準的虛擬文本,自從16世紀,當一個未知的打印機採取了與字母組成棒和炒它做的字體目錄。它已存活不僅五個世紀,但也幾乎沒有變化成電子排版。它是在20世紀60年代開始流行用含Lorem存有通道Letraset張的釋放,以及最近與桌面排版軟件,如奧爾德斯PageMaker中包括Lorem存有版本。如何合併論文3個JSON文件合併成一個單一的一個使用Ruby?

回答

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

一點題外話,在您發佈的代碼,你可以使用變量,他們不' t需要成爲實例變量。例如,category而非@category

+0

我遇到一些麻煩與第三級。一些二級類別沒有三級類別。但他們得到來自其他類別的衆多追隨者如何打破整個類別結構中增加。我用我用過的代碼更新了這個問題。什麼似乎是問題? – narzero 2014-08-28 11:17:45

+0

我錯了'需要第三級two_index'。遠眺third_from_second的數據,你只需要保持跟蹤你在該文件是什麼型號。 – 2014-08-28 15:25:34

+0

編輯的代碼大約顯示第三級別的代碼是什麼樣子。 – 2014-08-28 15:30:06

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

      ] 
     }, {...} 
相關問題