JSON Deserializing In C#

Hi, this blog is to show how to parse JSON content from a text file in ASP.NET and display the content in an ASP.NET GridView control.

Here, I have JSON content in 'json.txt' file and the content of my JSON text file is given below.
  1. /*O_o*/google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"978002229","table":{"cols":[{"id":"A","label":"","type":"number","pattern":"General"},{"id":"B","label":"","type":"string"},{"id":"C","label":"","type":"string"},{"id":"D","label":"","type":"string"},{"id":"E","label":"","type":"string"}],"rows":[{"c":[{"v":1.0,"f":"1"},{"v":"P24329"},{"v":"KMHC652568"},{"v":"971561696"},{"v":"Saad"}]},{"c":[{"v":7.0,"f":"7"},{"v":"A77198"},{"v":"JL5PG00376"},{"v":"97155142"},{"v":"TBI"}]},{"c":[{"v":8.0,"f":"8"},{"v":"G49533"},{"v":"JL5B302569"},{"v":"974079173"},{"v":"TBI"}]},{"c":[{"v":9.0,"f":"9"},{"v":"G51926"},{"v":"JL5D01721"},{"v":"971569184"},{"v":"TBI"}]},{"c":[{"v":10.0,"f":"10"},{"v":"F71102"},{"v":"JL5D04146"},{"v":"9715961"},{"v":"TBI"}]},{"c":[{"v":11.0,"f":"11"},{"v":"E77659"},{"v":"JTDB1064"},{"v":"9711903"},{"v":"Abu"}]},{"c":[{"v":12.0,"f":"12"},{"v":"D50124"},{"v":"JTDBZ003540"},{"v":"97157963"},{"v":"Abu"}]},{"c":[{"v":13.0,"f":"13"},{"v":"N42208"},{"v":"RK4416288"},{"v":"97156599"},{"v":"Abu"}]},{"c":[{"v":14.0,"f":"14"},{"v":"C56706"},{"v":"RKL412376"},{"v":"97156741"},{"v":"Abu"}]},{"c":[{"v":27.0,"f":"27"},{"v":"L26068"},{"v":"MDHB012207"},{"v":"9794105"},{"v":"TBI"}]}]}});   
First, we have to extract JSON part from the text file and use newtonsoft json deserializer. We can deserialise JSON data. To use newtonsoft, we need to add newtsoft.json package to our project through NuGet Package Manager Console.You can use http://json2csharp.com/ to create necessary class for your JSON data. Here is the complete code of the project. 
  1. using Newtonsoft.Json;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. namespace mywebapp {  
  11.     public partial class WebForm1: System.Web.UI.Page   
  12.     {#region Global Variable  
  13.         string JsonString = string.Empty;  
  14.         DataTable tb = new DataTable();  
  15.         DataRow dr;  
  16.         string json = string.Empty;  
  17.      #endregion  
  18.         protected void Page_Load(object sender, EventArgs e) {}  
  19.         protected void Button1_Click(object sender, EventArgs e) {  
  20.             try {  
  21.                 string FileName = "json.txt";  
  22.                 var stream = File.OpenText(Server.MapPath(FileName));  
  23.                 JsonString = stream.ReadToEnd();  
  24.                 json = JsonString;  
  25.                 json = json.Replace("/*O_o*/google.visualization.Query.setResponse({\"version\":\"0.6\",\"reqId\":\"0\",\"status\":\"ok\",\"sig\":\"978002229\",\"table\":"" ");  
  26.                 json = json.Replace("})""").Trim();  
  27.                 json = json.Replace(";""").Trim();  
  28.                 json = json.Replace("\"""\"").Trim();  
  29.                 tb.Columns.Add("Id"typeof(string));  
  30.                 tb.Columns.Add("Plate_No"typeof(string));  
  31.                 tb.Columns.Add("Engine_No"typeof(string));  
  32.                 tb.Columns.Add("Contact_id"typeof(string));  
  33.                 tb.Columns.Add("Owner_name"typeof(string));  
  34.                 GridView1.DataSource = tb;  
  35.                 GridView1.DataBind();  
  36.                 ViewState["table1"] = tb;  
  37.                 columns col = JsonConvert.DeserializeObject(json);  
  38.                 foreach(colsData c in col.cols) {}  
  39.                 int i = 0;  
  40.                 foreach(vfsArray v in col.rows) {  
  41.                     i++; // if you different keys for your values you dont need this  
  42.                     tb = (DataTable) ViewState["table1"];  
  43.                     dr = tb.NewRow();  
  44.                     foreach(vfs b in v.c) {  
  45.                         if (String.IsNullOrWhiteSpace(b.v)) {  
  46.                             break;  
  47.                         } else {  
  48.                             if (i == 1) {  
  49.                                 string value = b.v;  
  50.                                 dr["Id"] = "\t" + value;  
  51.                             }  
  52.                             if (i == 2) {  
  53.                                 string value = b.v;  
  54.                                 dr["Plate_No"] = "\t" + value;  
  55.                             }  
  56.                             if (i == 3) {  
  57.                                 string value = b.v;  
  58.                                 dr["Engine_No"] = "\t" + value;  
  59.                             }  
  60.                             if (i == 4) {  
  61.                                 string value = b.v;  
  62.                                 dr["Contact_id"] = "\t" + value;  
  63.                             }  
  64.                             if (i >= 5) {  
  65.                                 string value2 = b.v;  
  66.                                 dr["Owner_name"] = value2;  
  67.                             }  
  68.                             i++;  
  69.                         }  
  70.                     }  
  71.                     if (search.Text == (dr["owner_name"]).ToString()) {  
  72.                         tb.Rows.Add(dr);  
  73.                         GridView1.DataSource = tb;  
  74.                         GridView1.DataBind();  
  75.                     }  
  76.                     i = 0;  
  77.                 }  
  78.             } catch (Exception ex) {  
  79.                 Label1.Text = ex.ToString();  
  80.             }  
  81.         }  
  82.         protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e) {  
  83.             GridView1.PageIndex = e.NewPageIndex;  
  84.             this.DataBind();  
  85.         }  
  86.         protected void OnRowDataBound(object sender, GridViewRowEventArgs e) {}  
  87.         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {}  
  88.     }  
  89.     public class columns {  
  90.         public colsData[] cols {  
  91.             get;  
  92.             set;  
  93.         }  
  94.         public vfsArray[] rows {  
  95.             get;  
  96.             set;  
  97.         }  
  98.     }  
  99.     public class colsData {  
  100.         public String id {  
  101.             get;  
  102.             set;  
  103.         }  
  104.         public String label {  
  105.             get;  
  106.             set;  
  107.         }  
  108.         public String type {  
  109.             get;  
  110.             set;  
  111.         }  
  112.         public String pattern {  
  113.             get;  
  114.             set;  
  115.         }  
  116.     }  
  117.     public class vfsArray {  
  118.         public vfs[] c {  
  119.             get;  
  120.             set;  
  121.         }  
  122.     }  
  123.     public class vfs {  
  124.         public String v {  
  125.             get;  
  126.             set;  
  127.         }  
  128.         public String f {  
  129.             get;  
  130.             set;  
  131.         }  
  132.     }  
  133. }   
Build and run the project. 
Ebook Download
View all
Learn
View all