Click here to Skip to main content
15,887,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a json response that I will need to edit before binding to a GridView. The data binds perfectly to the GridView when "values" are removed from the string. When values is added to the json response I get this error message.

Message=Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.

How can I remove "values" from jsonResponse before binding to GridView?
ASP
<asp:GridView ID="gvJson" runat="server" BackColor="White" BorderColor="#3399ff"
            BorderStyle="Dotted" BorderWidth="1px" CellPadding="3" GridLines="Both" AutoGenerateColumns="true">
</asp:GridView>
C#
var jsonResponse = "{\"values\":[{\"id\":\"201\",\"name\":\"Zack\"},{\"id\":\"158\",\"name\":\"Kim\"},{\"id\":\"254\",\"name\":\"Scott\"}]}";

DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonResponse);

gvJson.DataSource = dataTable;
gvJson.DataBind();


What I have tried:

I know its conflicting with values and just need help to remove it. Thanks.
Posted
Updated 20-Jan-21 22:27pm
v3
Comments
Christian Graus 20-Jan-21 16:56pm    
What's wrong with the JSON?

If you remove 'values', it won't be value JSON, but you can string mash it to see if you like
 
Share this answer
 
That JSON represents a DataSet, not a DataTable. Deserialize to the correct type, and your code should work.
C#
var jsonResponse = "{\"values\":[{\"id\":\"201\",\"name\":\"Zack\"},{\"id\":\"158\",\"name\":\"Kim\"},{\"id\":\"254\",\"name\":\"Scott\"}]}";

DataSet ds = JsonConvert.DeserializeObject<DataSet>(jsonResponse);
gvJson.DataSource = ds.Tables[0];
gvJson.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900