2016-07-06 7 views
0

>CSVでCSVのみとめ列を使用してグループ化することにより、JSONオブジェクトを形成したかったがルックス:で(、)

Col_1 Col_2 Col_5 Col_6 

    1 YYY1 1111 CDX00001 
    1 YYY2 1111 CDX00001 

    2 YYY3 3333 CDX00002 

    2 YYY4 2222 CDX00001 

    2 YYY5 3333 CDX00002 
    2 YYY6 3333 CDX00002 
    2 YYY7 3333 CDX00002 
    2 YYY8 4444 CDX00002 
    2 YYY9 4444 CDX00002 

値をsperated> **私はへのコードの下に使用グループ2の列とJSONの結果は次のようになります。

 "[email protected]": [ 
     { 
      "Col_5": "4444", 
      "Col_6": "CDX00002", 
      "Col_1": "2", 
      "Col_2": "YYY8" 
     }, 
     { 
      "Col_5": "4444", 
      "Col_6": "CDX00002", 
      "Col_1": "2", 
      "Col_2": "YYY9" 
     } 
     ], 
     "[email protected]": [ 
     { 
      "Col_5": "3333", 
      "Col_6": "CDX00002", 
      "Col_1": "2", 
      "Col_2": "YYY3" 
     }, 
     { 
      "Col_5": "3333", 
      "Col_6": "CDX00002", 
      "Col_1": "2", 
      "Col_2": "YYY5" 
     }, 
     { 
      "Col_5": "3333", 
      "Col_6": "CDX00002", 
      "Col_1": "2", 
      "Col_2": "YYY6" 
     }, 
     { 
      "Col_5": "3333", 
      "Col_6": "CDX00002", 
      "Col_1": "2", 
      "Col_2": "YYY7" 
     } 
     ], 
     "[email protected]": [ 
     { 
      "Col_5": "1111", 
      "Col_6": "CDX00001", 
      "Col_1": "1", 
      "Col_2": "YYY1" 
     }, 
     { 
      "Col_5": "1111", 
      "Col_6": "CDX00001", 
      "Col_1": "1", 
      "Col_2": "YYY2" 
     }, 
     { 
      "Col_5": "1111", 
      "Col_6": "CDX00001", 
      "Col_1": "2", 
      "Col_2": "YYY10" 
     } 
     ], 
     "[email protected]": [ 
     { 
      "Col_5": "2222", 
      "Col_6": "CDX00001", 
      "Col_1": "2", 
      "Col_2": "YYY4" 
     } 
     ] 
    } 

>コードを使用:

   Map namesByCompany = list.stream() 
          .collect(Collectors.groupingBy(item->item.get("Col_6") +"[email protected]"+item.get("Col_5"),Collectors.toList())); 

      Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
      System.out.println("--result---"+gson.toJson(namesByCompany)); 

================================ =========================私はcsvファイルを持っているし、Java

詳細:

But problem here my groupBy columns is dynamic and cannot be hard coded as above, If we get another one columns then JSON should be prepared by considering new Groupby as well. I need to prepare a JSON from CSV somehow using dynamic gorupby columns. I tried in java but if you any other tools or coding please post here . Thanks in advance 

答えて

0

すべての行が一つのグループがある場合は、缶ジュース他の '、'が見つからなくなるまで、そのグループのJSONにグループを追加します。

+0

Delanoを詳しく説明できますか? – sathish

+0

あなたのデータがこのようになったら:2、YY9,44、CDX00002; がそうアレイ内のすべての部品を検索: 配列A [「2」、「yy9」、「44」、「cdx00002」] ので、あなたが最初の値を取り、次を探して、それを置きます配列を探して、その位置から次のものを探して、それらの間でデータを取得します。あなたが到達するまで続けてください。 そして、その配列からjsonをビルドします。 これは非常に効果がないかもしれませんが、わかりませんが、これは私が試みるものです。 –

+0

私もその方法を試しましたが、うまくいきませんでした。私を助けてくれる種類のコードはありますか? – sathish

関連する問題