6

I want to join objects of several classes together into a single JSON response, using Json.NET. I want to build the following into a single JSON response:

{
  "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"
}

I am creating JSON responses in the following way:

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

1 Answer 1

20

You can use anonymous types:

JsonConvert.SerializeObject(new {
    data = new[] { ... },
    summary = ...
});
Sign up to request clarification or add additional context in comments.

1 Comment

How would you do the revert operation though? ie. How would you deserialize the generated Json? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.