I’m working in ASP.NET and I’m getting this error when trying to serialize a JSON string into a DataTable:
Unexpected JSON token when reading DataTable. Expected StartArray, got
StartObject
I’ve read other posts that have this same error and it seems like the error normally occurs when the JSON object is invalid. However, I’ve validated my JSON in JsonLint and it is fine. I’m not constructing the JSON, I’m receiving it from the Paysimple API. I’m also using very similar methods in other parts of my software and it’s working fine, so I’m at a loss to explain why the error shows up here.
Any help is greatly appreciated. I don’t have a lot of experience working with JSON.
Here is my code:
protected void LoadPaymentSchedule(int customerId)
{
// This method returns an object from Paysimple's API
RecurringPaymentResponse recurringPayment = StudioPaymentAccess.GetStudioPaymentSchedule(customerId);
// Convert the Json response to DataTable
string a = JsonConvert.SerializeObject(recurringPayment);
DataTable tblSchedules = JsonConvert.DeserializeObject<DataTable>(a);//**error occurs here
// Bind to gridview
if (tblSchedules.Rows.Count > 0)
{
grdPaymentSchedules.DataSource = tblSchedules;
grdPaymentSchedules.DataBind();
}
else
{
//No schedules found
}
}
And here is the JSON that is returned by the method above StudioPaymentAccess.GetStudioPaymentSchedule(customerId)
{
"CustomerId": 1149814,
"CustomerFirstName": "Evan W",
"CustomerLastName": "Studio4",
"CustomerCompany": null,
"NextScheduleDate": "2020-11-16T07:00:00Z",
"PauseUntilDate": null,
"FirstPaymentDone": false,
"DateOfLastPaymentMade": "2020-10-16T06:00:00Z",
"TotalAmountPaid": 10.0,
"NumberOfPaymentsMade": 1,
"EndDate": "2021-10-16T06:00:00Z",
"PaymentAmount": 10.0,
"PaymentSubType": "Moto",
"AccountId": 1184647,
"InvoiceNumber": null,
"OrderId": null,
"FirstPaymentAmount": 0.0,
"FirstPaymentDate": null,
"StartDate": "2020-10-16T06:00:00Z",
"ScheduleStatus": "Active",
"ExecutionFrequencyType": "SpecificDayofMonth",
"ExecutionFrequencyParameter": 16,
"Description": " ",
"Id": 181512,
"LastModified": "2020-10-16T07:30:43Z",
"CreatedOn": "2020-10-15T18:11:22Z" }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It looks like in order to deserialize a DataTable, the expected JSON should be an array.
A quick work around do that would be to adjust the serialization so that it writes out an array:
string a = JsonConvert.SerializeObject(new[] { recurringPayment });
Another option would be to bind directly to return model, assuming that DataTable isn’t a requirement. For example, see the answer to Binding an ASP.NET GridView Control to a string array.
Method 2
Are you trying to Deserialize RecurringPaymentResponse Object to System.Data.DataTable?
It will definitely fail if you trying to Deserialize RecurringPaymentResponse Object to System.Data.DataTable. As properties of two class are not same.
You should directly update gridview rows
GridViewRow row = (GridViewRow) GridView1.Rows[0]; //First row row.Cells[0].Text = recurringPayment.CustomerId; row.Cells[1].Text = recurringPayment.CustomerFirstName; row.Cells[2].Text = recurringPayment.CustomerLastName; //and so on.....
DataTable Expects array as…
{
"data":
[
{"ID":1,"Name":"Simon"},
{"ID":2,"Name":"Alex"}
]
}
Method 3
I tryed to deserialize but don’t ocurred the error to me, the deserialize run successful.
https://i.stack.imgur.com/4tcns.png
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0