Bind JSON string in GridView using C# and VB.Net in ASP.Net

Amitabha
 
on Aug 22, 2021 11:10 PM
Sample_203811.zip
472 Views

Dear Sir

JSON to Bind Gridview

I have tried to Bind, But unable to do it.

This is the Body

{
   "code":200,
   "message":"Success",
   "onlineDataList":[
      {
         "address":"Perumpavur kotamanagalam road / Muvattupula/Ernakulam / KL",
         "dailyMileage":"0",
         "dateTime":1629666658000,
         "engine":false,
         "freeWheelAlert":false,
         "gpsdate":"23-08-2021 02:40:58",
         "haltDuration":"2281",
         "harshBrakeAlert":false,
         "idleAlert":false,
         "latitude":"9.929176",
         "longitude":"76.577295",
         "odometer":873894,
         "overSpeedAlert":false,
         "speed":0,
         "temperature":"26.5",
         "unitId":"100064",
         "vehicleId":"KA42A278"
      },
      {
         "address":"CMB Hub / TN",
         "dailyMileage":"115.61",
         "dateTime":1629696913000,
         "engine":false,
         "freeWheelAlert":false,
         "gpsdate":"23-08-2021 11:05:13",
         "haltDuration":"305",
         "harshBrakeAlert":false,
         "idleAlert":false,
         "latitude":"10.945945",
         "longitude":"76.997642",
         "odometer":862587,
         "overSpeedAlert":false,
         "speed":0,
         "temperature":"",
         "unitId":"100066",
         "vehicleId":"KA42A277"
      },
      {
         "address":"2.67KMs from Podanur/Station road / TN",
         "dailyMileage":"87.15",
         "dateTime":1629696905000,
         "engine":false,
         "freeWheelAlert":false,
         "gpsdate":"23-08-2021 11:05:05",
         "haltDuration":"174",
         "harshBrakeAlert":false,
         "idleAlert":false,
         "latitude":"10.94286",
         "longitude":"76.999238",
         "odometer":683275,
         "overSpeedAlert":false,
         "speed":0,
         "temperature":"38.0",
         "unitId":"100067",
         "vehicleId":"KA42A7504"
      },
      {
         "address":"CMB Hub / TN",
         "dailyMileage":"0",
         "dateTime":1629685546000,
         "engine":false,
         "freeWheelAlert":false,
         "gpsdate":"23-08-2021 07:55:46",
         "haltDuration":"1101",
         "harshBrakeAlert":false,
         "idleAlert":false,
         "latitude":"10.945816",
         "longitude":"76.997607",
         "odometer":2771595,
         "overSpeedAlert":false,
         "speed":0,
         "temperature":"24.0",
         "unitId":"100201",
         "vehicleId":"KA42A9832"
      }
   ],
   "vehicleId":"ALL"
}

 

IRestResponse response = client.Execute(request);
string ms = response.Content;
root EResponse = JsonConvert.DeserializeObject<root>(ms);
if (EResponse.message.ToString() == "Success")
{
    GridView1.DataSource = EResponse.result.vehicleId;
    GridView1.AutoGenerateColumns = true;
    if (GridView1 != null)
    {
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 23, 2021 01:16 AM

Hi Amitabha,

Check this example. Now please take its reference and correct your code.

HTML

<asp:GridView runat="server" ID="GridView1"></asp:GridView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    // Read the json string using IRestResponse.
    string ms = System.IO.File.ReadAllText(Server.MapPath("~/json.json"));
    Vehicle EResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<Vehicle>(ms);
    if (EResponse.message.ToString() == "Success")
    {
        GridView1.AutoGenerateColumns = true;
        GridView1.DataSource = EResponse.onlineDataList.Select(x=>x.vehicleId);
        GridView1.DataBind();
    }
}


public class Vehicle
{
    public int code { get; set; }
    public string message { get; set; }
    public List<OnlineDataList> onlineDataList { get; set; }
    public string vehicleId { get; set; }
}

public class OnlineDataList
{
    public string address { get; set; }
    public string dailyMileage { get; set; }
    public object dateTime { get; set; }
    public bool engine { get; set; }
    public bool freeWheelAlert { get; set; }
    public string gpsdate { get; set; }
    public string haltDuration { get; set; }
    public bool harshBrakeAlert { get; set; }
    public bool idleAlert { get; set; }
    public string latitude { get; set; }
    public string longitude { get; set; }
    public int odometer { get; set; }
    public bool overSpeedAlert { get; set; }
    public int speed { get; set; }
    public string temperature { get; set; }
    public string unitId { get; set; }
    public string vehicleId { get; set; }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    ' Read the json string using IRestResponse.
    Dim ms As String = IO.File.ReadAllText(Server.MapPath("~/json.json"))
    Dim EResponse As Vehicle = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vehicle)(ms)
    If EResponse.message.ToString() = "Success" Then
        GridView1.AutoGenerateColumns = True
        GridView1.DataSource = EResponse.onlineDataList.Select(Function(x) x.vehicleId)
        GridView1.DataBind()
    End If
End Sub

Public Class Vehicle
    Public Property code As Integer
    Public Property message As String
    Public Property onlineDataList As List(Of OnlineDataList)
    Public Property vehicleId As String
End Class

Public Class OnlineDataList
    Public Property address As String
    Public Property dailyMileage As String
    Public Property dateTime As Object
    Public Property engine As Boolean
    Public Property freeWheelAlert As Boolean
    Public Property gpsdate As String
    Public Property haltDuration As String
    Public Property harshBrakeAlert As Boolean
    Public Property idleAlert As Boolean
    Public Property latitude As String
    Public Property longitude As String
    Public Property odometer As Integer
    Public Property overSpeedAlert As Boolean
    Public Property speed As Integer
    Public Property temperature As String
    Public Property unitId As String
    Public Property vehicleId As String
End Class

Output

Item
KA42A278
KA42A277
KA42A7504
KA42A9832