Return datatable or dataset in json format in wcf web service in c#

This is my code:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, 
           RequestFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.Wrapped, 
           UriTemplate = "DoWork")]
public string DoWork()
{
        DataSet ds = new DataSet();

        DataTable dtStatus = new DataTable("Status");
        dtStatus.Columns.Add("status");
        dtStatus.Columns.Add("message");
        DataRow dr = dtStatus.NewRow();
        dr["status"] = "true";
        dr["message"] = "Logged in Successfully.";
        dtStatus.Rows.Add(dr);

        ds.Tables.Add(dtStatus);

        DataTable dtData = new DataTable("data");
        dtData.Columns.Add("ID");
        dtData.Columns.Add("email");
        dtData.Columns.Add("Name");
        DataRow drData = dtData.NewRow();
        drData["ID"] = "1";
        drData["email"] = "test@test.com";
        drData["Name"] = "Test";
        dtData.Rows.Add(drData);

        ds.Tables.Add(dtData);

        return JsonConvert.SerializeObject(ds);
}

Result should be this JSON data:

{

data:    {
       " email" : "rohit@gmail.com",
        "Name": 3,
        "ID" : 5
    },

 "message": "Logged in Successfully.";
    "status" : true
}

Related posts

Recent Viewed