I wanted to display the data from JSON file to the gridview. I've managed to decode the JSON file and I was trying to bind with the gridview.
However, an error pops out.
The error is: Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1
The JSON code:
{
"value":{
"Status": 2,
"AffectedSegments": [
{
"Line": "NEL",
"Direction": "HarbourFront",
"Stations": "NE9,NE8,NE7,NE6",
"MRTShuttleDirection": "HarbourFront"}
,
{
"Line": "EWL",
"Direction": "Simei",
"Stations": "NE9,NE8,NE7,NE6",
"MRTShuttleDirection": "HarbourFront"}],
"Message": [
{
"Content": "0901hrs : NEL "
"CreatedDate": "2018-03-16 09:01:53"
}
]
}
}
The code:
public DataTable jsonDataDiplay()
{
StreamReader sr = new StreamReader(Server.MapPath("TrainServiceAlerts.json"));
string json = sr.ReadToEnd();
var table = JsonConvert.DeserializeObject<DataTable>(json);
//DataSet ds = JsonConvert.DeserializeObject<Wrapper>(json).DataSet;
return table;
}
The design page:
<asp:GridView ID="GridView2" runat="server">
<Columns>
<asp:BoundField DataField="Line" HeaderText="Line" />
<asp:BoundField DataField="Direction" HeaderText="Direction" />
<asp:BoundField DataField="Stations" HeaderText="Stations" />
<asp:BoundField DataField="MRTShuttleDirection" HeaderText="MRTShuttleDirection" />
</Columns>
</asp:GridView>
I'm not sure how to solve the error. Please, help me and advise me! i have added " besides NE. It was there from the start in my json file just tht i didnt copy correctly here.
Thank you in advance!