How To Display JSON Data Into GridView

Mansoor Ahmed 1 Reputation point
2022-01-30T12:57:08.4+00:00

Dear Experts, I Have This Json Data From Firebase Real Time Data Base.

Could You Please Show Me A Single Example How Can I Display This Data Into GridView C# Without KEY.

{
    "-MtmDfxQyVwkr7SkHsrg": {
        "amount": "500000",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "4245",
        "voucher_date": "2022-01-19T15:05:46",
        "voucher_number": "156202200001"
    },
    "-MtmE1Y54t3G_wLTuICr": {
        "amount": "50000",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "4245",
        "voucher_date": "2022-01-19T15:07:22",
        "voucher_number": "156202200002"
    },
    "-MtmEilHXO54Qqai1CGx": {
        "amount": "125000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "4245",
        "voucher_date": "2022-01-19T15:10:26",
        "voucher_number": "156202200003"
    },
    "-MtpxcPYxmXumoqATUFi": {
        "amount": "568847.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "108",
        "voucher_date": "2022-01-20T08:29:36",
        "voucher_number": "156202200004"
    },
    "-MtpxwVLqAOpVaigM7bU": {
        "amount": "568847.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "108",
        "voucher_date": "2022-01-20T08:31:04",
        "voucher_number": "156202200005"
    },
    "-Mtq-AYxYykwtl0Ff1mH": {
        "amount": "4658000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "108",
        "voucher_date": "2022-01-20T08:40:55",
        "voucher_number": "156202200006"
    },
    "-Mtq-bB71DcjDgF0TeMl": {
        "amount": "4658000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "108",
        "voucher_date": "2022-01-20T08:42:52",
        "voucher_number": "156202200007"
    },
    "-Mu54ozWi8hO6_lpkaeC": {
        "amount": "100000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "7845",
        "voucher_date": "2022-01-23T11:37:47",
        "voucher_number": "156202200008"
    },
    "-Mu55GfMw0puE2185qxT": {
        "amount": "1000000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "7845",
        "voucher_date": "2022-01-23T11:41:34",
        "voucher_number": "156202200009"
    },
    "-Mu55hfHxhEHQc-FAUHP": {
        "amount": "1.2000050",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "4245",
        "voucher_date": "2022-01-23T11:43:19",
        "voucher_number": "156202200010"
    },
    "-Mu56Qk6Nmfu1phGA-Uh": {
        "amount": "120050.5",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "4245",
        "voucher_date": "2022-01-23T11:46:30",
        "voucher_number": "156202200011"
    },
    "-Mu9yUlTL6UdVgUEFQ3Z": {
        "amount": "125000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "4245",
        "voucher_date": "2022-01-24T10:18:30",
        "voucher_number": "156202200012"
    },
    "-MuPAXq2OWEiEfOYLOvc": {
        "amount": "784569.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "7845",
        "voucher_date": "2022-01-27T09:13:27",
        "voucher_number": "156202200013"
    },
    "-MuPMd1HznpTRwNlSDbS": {
        "amount": "4587126.0",
        "branch_num": "005",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "4245",
        "voucher_date": "2022-01-27T09:36:51",
        "voucher_number": "156202200014"
    },
    "-MuPW5cMw4ICK3ZoiHZC": {
        "amount": "5959.0",
        "branch_num": "004",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "7845",
        "voucher_date": "2022-01-27T10:50:48",
        "voucher_number": "156202200015"
    },
    "-MuPYJ53-EGGPzWvszhy": {
        "amount": "568932.0",
        "branch_num": "005",
        "machineID": "VB12213C20156",
        "shift": 1,
        "userID": "7845",
        "voucher_date": "2022-01-27T11:00:35",
        "voucher_number": "156202200016"
    },
    "-MuPh0RFdkXA6Jw5wYxp": {
        "amount": "10000.0",
        "branch_num": "001",
        "machineID": "VB12213C20156",
        "shift": 2,
        "userID": "7",
        "voucher_date": "2022-01-27T11:42:56",
        "voucher_number": "156202200017"
    }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,243 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,061 Reputation points
    2022-01-31T02:20:43.707+00:00

    Hi @Mansoor Ahmed ,
    According to your description,you can not deserialize your json directly to a datatable. Your data sits deep inside the hierachical structure, so you have to do a bit more work to convert this:

    public DataTable jsonDataDiplay()  
    {  
        StreamReader sr = new StreamReader(Server.MapPath("TrainServiceAlerts.json"));  
        string json = sr.ReadToEnd();  
        dynamic table = JsonConvert.DeserializeObject(json);  
        DataTable newTable = new DataTable();  
        newTable.Columns.Add("amount", typeof(string));  
        newTable.Columns.Add("branch_num", typeof(string));  
        newTable.Columns.Add("machineID", typeof(string));  
        newTable.Columns.Add("shift", typeof(string));  
        newTable.Columns.Add("userID", typeof(string));  
        newTable.Columns.Add("voucher_date", typeof(string));  
        newTable.Columns.Add("voucher_number", typeof(string));  
      
        foreach (var row in table.value.data)  
        {  
            newTable.Rows.Add(row.amount, row.branch_num, row.machineID, row.shift,row.userID,row.voucher_date,row.voucher_number);  
        }  
        return newTable;  
    }  
    GridView.DataSource = newTable ;  
    GridView.DataBind();  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Mansoor Ahmed 1 Reputation point
    2022-01-31T08:54:06.107+00:00

    Is There Any Way That Can Achieve Something Like That Using URL Not A File From Server.

     **StreamReader sr = new StreamReader(Server.MapPath("https://receipt-voucher-default-rtdb.firebaseio.com/receipts.json));**
    
         StreamReader sr = new StreamReader(Server.MapPath("TrainServiceAlerts.json"));
    

  3. Jose Zero 576 Reputation points
    2022-01-31T12:47:14.96+00:00

    @Mansoor Ahmed
    WebClient provides an simple way to get a string from URL, sample should return your external IP address like {"ip":"aaa.bbb.ccc.ddd"}
    system.net.webclient

    System.Net.WebClient MyClient = new System.Net.WebClient();  
    string MyString = null;  
    try  
    {  
      MyString = MyClient.DownloadString("https://api.ipify.org/?format=json");  
    }  
    catch (System.Net.WebException ex)  
    {  
      MyString = "Error: " + ex.Message;  
    }  
    

  4. Mansoor Ahmed 1 Reputation point
    2022-02-02T06:25:42.22+00:00
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _5M_Solution
    {
      public partial class Harqat_Maqbozat_Grid : System.Web.UI.Page
      {
        protected void Page_Load(object sender, EventArgs e)
        {
          if (!this.IsPostBack)
          {
            this.GetJsonData();
          }
        }
    
        public void GetJsonData()
        {
          ServicePointManager.Expect100Continue = true;
          ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
          string json = (new WebClient()).DownloadString("https://receipt-voucher-default-rtdb.firebaseio.com/receipts.json");
          Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
          List<Receipt> receipts = new List<Receipt>();
          foreach (var entry in values)
          {
            receipts.Add(JsonConvert.DeserializeObject<Receipt>(entry.Value.ToString()));
          }
          gvDetails.DataSource = receipts;
          gvDetails.DataBind();
        }
    
        public class Receipt
        {
          public string amount { get; set; }
          public string branch_num { get; set; }
          public string machineID { get; set; }
          public string shift { get; set; }
          public string userID { get; set; }
          public string voucher_date { get; set; }
          public string voucher_number { get; set; }
        }
    
        public void ExportExcel()
        {
          try
          {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "5M_MaqbozatDetails.xls"));
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;
            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
            GetJsonData();
            //dl_JsonDetails.DataBind();
            gvDetails.RenderControl(html);
            Response.Write(writer.ToString());
            Response.Flush();
            Response.End();
          }
          catch (Exception ex)
          {
          }
        }
    
        protected void btn_ExportExcel_Click(object sender, EventArgs e)
        {
          ExportExcel();
        }
    
        protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
          gvDetails.PageIndex = e.NewPageIndex;
          this.GetJsonData();
        }
    
        protected void dd_SearchByBranchNo_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
      }
    }
    

  5. SurferOnWww 1,906 Reputation points
    2022-07-31T01:49:09.493+00:00

    I suggest that you use the DynamicJson to deserialize the JSON string and to obtain the data source of List<T> which can be shown on the GridView.

    226396-dynamicjson.jpg

    A sample code:

    using System;  
    using System.Collections.Generic;  
    using System.IO;  
    using Codeplex.Data;  
      
    namespace DynamicJsonSample  
    {  
        internal class Program  
        {  
            static void Main(string[] args)  
            {  
                string filepath = @"C:\Users\surfe\Documents\Visual Studio 2022\CsProject\DynamicJson\DynamicJson\JsonFile.json";  
                string jsonText = "";  
                List<Item> list = new List<Item>();  
      
                using (StreamReader sr = File.OpenText(filepath))  
                {  
                    jsonText = sr.ReadToEnd();  
                }  
      
                var json = DynamicJson.Parse(jsonText);  
      
                foreach (KeyValuePair<string, dynamic> kvp in json)  
                {  
                    Item item = new Item  
                    {  
                        amount = kvp.Value.amount,  
                        branch_num = kvp.Value.branch_num,  
                        machineID = kvp.Value.machineID,  
                        shift = (int)kvp.Value.shift,  
                        userID = kvp.Value.userID,  
                        voucher_date = DateTime.Parse(kvp.Value.voucher_date),  
                        voucher_number = kvp.Value.voucher_number  
                    };  
                    list.Add(item);  
                }  
            }  
        }  
      
        public class Item  
        {  
            public string amount { get; set; }  
            public string branch_num { get; set; }  
            public string machineID { get; set; }  
            public int shift { get; set; }  
            public string userID { get; set; }  
            public DateTime voucher_date { get; set; }  
            public string voucher_number { get; set; }  
        }  
    

    Result:

    226409-result.jpg

    0 comments No comments