0
{
  "success": 1,
  "items": [
    {
      "ItemID": "13",
      "ItemName": "Diamond (50 Kg PP)",
      "ItemDesc": "",
      "MRP": "0",
      "Rate": "0",
      "Unit": "",
      "Weight": "50",
      "ItemGroup": ""
    },
    {
      "ItemID": "8",
      "ItemName": "Extra Milk (59 Kg Jute)",
      "ItemDesc": "",
      "MRP": "0",
      "Rate": "0",
      "Unit": "",
      "Weight": "59",
      "ItemGroup": ""
    },
    {
      "ItemID": "19",
      "ItemName": "Extra Milk (59 Kg PP)",
      "ItemDesc": "",
      "MRP": "0",
      "Rate": "0",
      "Unit": "",
      "Weight": "59",
      "ItemGroup": ""
    },
    {
      "ItemID": "23",
      "ItemName": "Test222",
      "ItemDesc": "",
      "MRP": "0",
      "Rate": "0",
      "Unit": "",
      "Weight": "50",
      "ItemGroup": ""
    }
  ]
}

my code

public  async void myweb()
{
    string vJ = "";
    string url = "https://crmscf.vidyasystems.com/api/gen/items.php";
    var client = new RestClient(url);
    var request = new RestRequest();
           
    var responce = client.Get(request);
    // Console.WriteLine(responce.Content.ToString());
    // txtJson.Text = responce.Content.ToString();             

    vJ =  responce.Content.ToString();
    DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(@vJ, (typeof(DataTable)));
    dgvXml.DataSource = dataTable;
}

I am getting error :

Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.'

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
  • Your JSON does not represent datatable. So you can not deserialize it to datatable. You should create a class structure for your JSON. You can use https://json2csharp.com/. Also you should consider loading data in to the gridview from a List instead of DataTable. This will ease lot of things for you. – Chetan Aug 15 '22 at 05:50
  • I have tried to de serialize it class Item, but it is giving Error of invalid charter, if i replace "" with '' it is running well. But as i am reading response from API I have to process as I am getting. – Ketan Patil Aug 15 '22 at 07:48
  • Can you share the code which you tried to deserialize from JSON to class? what do you mean by replaceing `""` to `''`. You mean replacing in JSON? There are not `''` in the JSON you shared. What am I missing here? – Chetan Aug 15 '22 at 08:21
  • public class stkItem { 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; } } – Ketan Patil Aug 15 '22 at 09:57
  • stkItem stkIt = Newtonsoft.Json.JsonConvert.DeserializeObject(vJ); but stkIt getting value {"ItemID":null,"ItemName":null,"ItemDesc":null,"MRP":null,"Rate":null,"Unit":null,"Weight":null,"ItemGroup":null} – Ketan Patil Aug 15 '22 at 09:58

1 Answers1

0

The following uses a different method to read json. The models and helper class belong in their own files.

enter image description here

enter image description here

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace DemoApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ReadJsonButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (var wc = new WebClient())
                {
                    var json = wc.DownloadString("https://crmscf.vidyasystems.com/api/gen/items.php");
                    Container container = JsonConvert.DeserializeObject<Container>(json);
                    if (container != null && container.success == 1)
                    {
                        var dt = container.items.ToList().ToDataTable();
                    }
                    else
                    {
                        // deal with it
                    }
                }
            }
            catch (Exception ex)
            {
                // deal with error
            }

        }
    }


    public class Container
    {
        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; }
    }

    public static class Operations
    {
        public static DataTable ToDataTable<T>(this IList<T> list) 
            => (DataTable)JsonConvert
                .DeserializeObject(JsonConvert.SerializeObject(list), (typeof(DataTable)));
    }

}
Karen Payne
  • 4,341
  • 2
  • 14
  • 31