Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to convert json result into datatable c#

DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
Comment

how to convert json result into datatable c#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Linq;
...

public static DataTable Tabulate(string json)
{
    var jsonLinq = JObject.Parse(json);

    // Find the first array using Linq
    var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
    var trgArray = new JArray();
    foreach (JObject row in srcArray.Children<JObject>())
    {
        var cleanRow = new JObject();
        foreach (JProperty column in row.Properties())
        {
            // Only include JValue types
            if (column.Value is JValue)
            {
                cleanRow.Add(column.Name, column.Value);
            }
        }

        trgArray.Add(cleanRow);
    }

    return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
}
Comment

C# Convert Json File to DataTable

 public DataTable Read(string filePath)
        {
            try
            {
                //check if json structure is okay (http://jsonlint.com)
                //generate object class http://www.jsonutils.com
             
                DataTable dt = new DataTable();
                dt = JsonConvert.DeserializeObject<DataTable>(File.ReadAllText(filePath));

                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert iso string to datetime javascript 
Javascript :: add class when element in viewport vanilla javascript 
Javascript :: set multiple attributes css javascript 
Javascript :: js function pick properties from object 
Javascript :: react-native-permissions could not be found within the project or in these directories: 
Javascript :: document print from html javascript 
Javascript :: reload page in react router dom v6 
Javascript :: append object to object javascript 
Javascript :: delete multiple keys from object javascript 
Javascript :: import typography react 
Javascript :: multiple line string javascript 
Javascript :: checkbox change event javascript 
Javascript :: datatables add row 
Javascript :: metamask event disconnect 
Javascript :: how to add button react native app.js 
Javascript :: js window.confirm 
Javascript :: base 64 in js 
Javascript :: how to remove key value pair from object js 
Javascript :: javascript capitalize array 
Javascript :: js map add property 
Javascript :: REACT-ICONS reduce thickness 
Javascript :: .find() is not a function 
Javascript :: can i pass data with usenavigate react router 
Javascript :: golang parse jason 
Javascript :: how to include a css file in jsp 
Javascript :: How to call a c# functio from Javascript 
Javascript :: datatables get all rows 
Javascript :: change no to string in js 
Javascript :: react native image blur 
Javascript :: random rgba color javascript except black 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =