6

我想使用 Json.NET 将多个类的对象连接到一个 JSON 响应中。我想将以下内容构建到单个 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);
4

1 回答 1

16

您可以使用匿名类型:

JsonConvert.SerializeObject(new {
    data = new[] { ... },
    summary = ...
});
于 2012-04-22T13:47:15.080 回答