2012-04-22 44 views
2

我想使用Json.NET將幾個類的對象一起連接成單個JSON響應。我想建立以下到一個單一的JSON響應:使用Json.NET將多個類的對象序列化爲單個JSON

{ 
    "data": [ 
    { 
     "from": { 
     "name": "Pawan Shrestha", 
     "id": "100001187416487" 
     }, 
     "created_time": "2012-04-22T10:21:22+0000", 
     "unread": true, 
     "to": { 
     "name": "Shashwat Tripathi", 
     "id": "100000559654730" 
     } 
    } 
    ], 
    "summary": { 
    "unread_count": 1, 
    "total_count": 1, 
    "updated_time": "2012-04-22T10:21:22+0000" 
    } 
    "response_generated_on" : "2012-04-12 14:23:33" 
} 

我通過以下方式創建JSON響應:

Customer cs = new Customer(2011); //2011 is Customer A/c No. 
string j = JsonConvert.SerializeObject(cs); 

回答

6

您可以使用匿名類型:

JsonConvert.SerializeObject(new { 
    data = new[] { ... }, 
    summary = ... 
}); 
相關問題