Get Json from DataTable - CSharp System.Runtime.Serialization.Json

CSharp examples for System.Runtime.Serialization.Json:Json

Description

Get Json from DataTable

Demo Code


using System.Web.Script.Serialization;
using System.Reflection.Emit;
using System.Reflection;
using System.Data;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;/*from  ww w  . j a v  a 2s.c o m*/

public class Main{
        public static string GetJson(DataTable dt)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<string[]> list = new List<string[]>();
            
            string[] stringArr = null;
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
              
                stringArr = new string[dt.Columns.Count];
                i = 0; 
                foreach (DataColumn col in dt.Columns)
                {
                   
                    stringArr[i] = dr[col].ToString();
                    i++;
                }
                list.Add(stringArr);
            }
            return serializer.Serialize(list);
        }
}

Related Tutorials