2016-10-21 13 views
0

データテーブルからJSON文字列を作成し、目的のネストされた出力からクラスを生成しました。文字列をネストされたオブジェクトに変換するための背後にあるシャープコードの助けが必要です。このために別注のシリアライザを作成する必要がありますか?より良い方法はありますか?csクラスを使用してフラットファイルをネストJSONに変換する方法

string = [{"name":"example1", "priority" : "high", "PHASE": "phase1","id" :1001, "type": "glove"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase1","id" :1002, "type": "shoe"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase1","id" :1003, "type": "sock"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase2","id" :1005, "type": "large"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase2","id" :1006, "type": "medium"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase2","id" :1007, "type": "small"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase3","id" :1008, "type": "ladies"}, 
{"name":"example1", "priority" : "high", "PHASE": "phase3","id" :1009, "type": "gents"}]; 



desired_output = { 
"NAME": "example1", 
"PRIORITY" : "high", 
"PHASE": 
      { 
      "phase1": 
         [ 
         { "id": "1001", "type": "glove" }, 
       { "id": "1002", "type": "shoe" }, 
       { "id": "1003", "type": "sock", 

"ph2": 
      { 
       "phase2" : 
         [ 
         { "id": "1005", "type": "large" }, 
       { "id": "1006", "type": "medium" }, 
       { "id": "1007", "type": "small", 
"ph3": 
      { 
      "phase3": 
         [ 
         { "id": "1008", "type": "ladies" }, 
       { "id": "1009", "type": "gents" } 

]} 
} 
]} 
} 
] 
} 
} 


public class Phase3 
{ 
    public string id { get; set; } 
    public string type { get; set; } 
} 

public class Ph3 
{ 
    public List<Phase3> phase3 { get; set; } 
} 

public class Phase2 
{ 
    public string id { get; set; } 
    public string type { get; set; } 
    public Ph3 ph3 { get; set; } 
} 

public class Ph2 
{ 
    public List<Phase2> phase2 { get; set; } 
} 

public class Phase1 
{ 
    public string id { get; set; } 
    public string type { get; set; } 
    public Ph2 ph2 { get; set; } 
} 

public class PHASE 
{ 
    public List<Phase1> phase1 { get; set; } 
} 

public class RootObject 
{ 
    public string NAME { get; set; } 
    public string PRIORITY { get; set; } 
    public PHASE PHASE { get; set; } 
} 
+0

あなたはphがネストされている必要がありますか、別のオブジェクト(階層的にph1> ph2> ph3> ph4か同じレベルのphですか?)をしたいですか? – Tinwor

+0

こんにちは。 ph1> ph2> ph3> ph4である。 – user3359706

答えて

0

あなたはRootObject1は次のように定義されてList<RootObject1>にあなたのJSONを解析する必要がまず第一に:あなたはイースリーJSON.Net内部JsonConvert.DeserializeObject<T>(string)を使用してこの作業を行ってでき

public class RootObject1 
{ 
    public string name { get; set; } 
    public string priority { get; set; } 
    public string PHASE { get; set; } 
    public int id { get; set; } 
    public string type { get; set; } 
} 


リストを取得したら、PHASEでリストをPHASEでグループ化できます。コード、それはこのようなものです:今、あなたは、各グループに行くと(私は間違っていないよIOF種類によっておよび順序)適切なクラスを埋めるためのforeachを必要とする(IENumerable<IGrouping<string,RootObject1>>として定義)のグループを持っていることを

var groups = myList.GroupBy(x => x.PHASE); 


foreachサイクルが終了したら、RootObjectをシリアル化し、必要に応じてjsonをフォーマットします。
ヒント:PhaseNassaysでなければなりません。正しい結果が得られません。

+0

クラスをグループ化して入力する方法について、より多くのヘルプを提供してください。 – user3359706

関連する問題