I am trying to convert a json response from REST API into a DataTable
. The following is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
using Newtonsoft.Json;
using System.Data;
namespace RestDemo1
{
class Program
{
static void Main(string[] args)
{
getEmployeeData();
}
public static void getEmployeeData()
{
var client = new RestClient("https://crmscf.vidyasystems.com/api/gen/items.php");
var request = new RestRequest("items");
var response = client.Execute(request);
string vJ = "";
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string rawResponse = response.Content;
DataTable tester = (DataTable)JsonConvert.DeserializeObject(rawResponse, (typeof(DataTable)));
}
}
public class Rootobject
{
public int success { get; set; }
public Item[] items { get; set; }
public class Item
{
public string ItemID { get; set; }
public string ItemName { get; set; }
public string ItemDesc { get; set; }
public string MRP { get; set; }
public string Rate { get; set; }
public string Unit { get; set; }
public string Weight { get; set; }
public string ItemGroup { get; set; }
}
}
}
}
When I try get the data into a table format, I get error:
DataTable tester = (DataTable)JsonConvert.DeserializeObject(rawResponse, (typeof(DataTable)));
Error message- Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.'