locked
Convert datatable to json string RRS feed

  • Question

  • User582566331 posted

    I would like to convert datatable to json string but its coming as follows:

    [

      {

        "Categoryid": "1LV",

        "Categoryname": "LV Testing",

        "Description": "LV Testing Category"

      },

      {

        "Categoryid": "1SW",

        "Categoryname": "SW Testing",

        "Description": "SW Testing Category"

      },

      {

        "Categoryid": "RP1",

        "Categoryname": "Reporting",

        "Description": "Reporting Category"

      },

      {

        "Categoryid": "RS1",

        "Categoryname": "Resizing",

        "Description": "Resizing Category"

      }

    ]

    I would like to insert table name and  the following also in the beginning

    {
    "responseStatus": {
    "value": "SUCCESS",
    },

    Please help me to do it.

    Tuesday, November 6, 2018 12:44 PM

Answers

  • User61956409 posted

    Hi sweetyPaul,

    would like to insert table name and  the following also in the beginning

    To achieve the requirement, you can refer to the following code snippet.

    [Route("api/dtdata")]
    [HttpGet]
    public IHttpActionResult GetDTData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Categoryid");
        dt.Columns.Add("Categoryname");
        dt.Columns.Add("Description");
    
        dt.Rows.Add("1LV", "LV Testing", "LV Testing Category");
        dt.Rows.Add("1SW", "SW Testing", "SW Testing Category");
    
        return Ok(new { responseStatus = new { value = "SUCCESS" }, tableName = "testtable", tableRecords = dt });
    }

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 7, 2018 7:49 AM

All replies

  • User1120430333 posted

    I would like to convert datatable to json string but its coming as follows:

    So, you are saying that you are trying to send an ADO.NET Datatable  using a WebAPI in Json format? That's not optimal programming. Json means JavaScript Object Notation. Json works best when using a custom object type like a DTO that can be Json serialized and deserialized  easily with the objects being in a collection like a List<T>.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    Tuesday, November 6, 2018 2:40 PM
  • User61956409 posted

    Hi sweetyPaul,

    would like to insert table name and  the following also in the beginning

    To achieve the requirement, you can refer to the following code snippet.

    [Route("api/dtdata")]
    [HttpGet]
    public IHttpActionResult GetDTData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Categoryid");
        dt.Columns.Add("Categoryname");
        dt.Columns.Add("Description");
    
        dt.Rows.Add("1LV", "LV Testing", "LV Testing Category");
        dt.Rows.Add("1SW", "SW Testing", "SW Testing Category");
    
        return Ok(new { responseStatus = new { value = "SUCCESS" }, tableName = "testtable", tableRecords = dt });
    }

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 7, 2018 7:49 AM