Home > Backend Development > C++ > How to Convert a DataTable to a JSON String with a 'records' Array in C#?

How to Convert a DataTable to a JSON String with a 'records' Array in C#?

Patricia Arquette
Release: 2025-01-21 16:36:10
Original
383 people have browsed it

How to Convert a DataTable to a JSON String with a

C# DataTable to JSON Conversion: Achieving the Desired Format

This guide demonstrates how to convert a C# DataTable into a JSON string with a specific structure, avoiding the default JSON object format. The target format is a JSON object containing a "records" array of individual record objects.

Steps:

  1. Populate the DataTable: Begin by creating a DataTable and populating it with data from your database (or any other source).

  2. Use JavaScriptSerializer: Utilize the JavaScriptSerializer class to handle the JSON serialization.

  3. Create a List of Dictionaries: Construct a list to store each row of the DataTable as a dictionary.

  4. Iterate and Populate Dictionaries: Loop through each DataRow in the DataTable. For each row:

    • Create a new dictionary.
    • Iterate through each DataColumn in the DataTable.
    • Add the column name (col.ColumnName) as the key and the corresponding column value (dr[col]) as the value to the dictionary.
    • Add the completed dictionary to the list of dictionaries.
  5. Serialize the List: Finally, serialize the list of dictionaries using JavaScriptSerializer, wrapping it in a JSON object with a "records" property.

Code Example:

<code class="language-csharp">using System.Web.Script.Serialization;
using System.Data;
using System.Collections.Generic;

public string ConvertDataTableToJson()
{
    DataTable dataTable = new DataTable();
    // Fill dataTable with data from your database

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    List<Dictionary<string, object>> records = new List<Dictionary<string, object>>();

    foreach (DataRow row in dataTable.Rows)
    {
        Dictionary<string, object> record = new Dictionary<string, object>();
        foreach (DataColumn column in dataTable.Columns)
        {
            record.Add(column.ColumnName, row[column]);
        }
        records.Add(record);
    }

    return serializer.Serialize(new { records = records });
}</code>
Copy after login

This revised code provides a clear and efficient method for converting a DataTable to the specified JSON format, ensuring each record is properly represented within a "records" array. Remember to add the necessary using statements.

The above is the detailed content of How to Convert a DataTable to a JSON String with a 'records' Array in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template