Whenever I receive dynamic JSON data, it could be a JSON array, a simple JSON object, or nested JSON objects.
I am attempting to deserialize and convert the JSON object/array into a DataTable for further processing using Newtonsoft.Json.
Here is my current code:
public DataTable ConvertJson2DataTable(string jdata)
{
DataTable dt = null;
try {
dt = (DataTable)JsonConvert.DeserializeObject(jdata, typeof(DataTable));
}
catch (Exception ex)
{
MessageBox.Show("exception: " +ex.Message.ToString());
}
return dt;
}
It works well for regular JSON objects and arrays. However, when dealing with complex JSON objects, I encounter an error message stating "Additional text found in JSON string after finishing deserializing object."
Below is a sample of the JSON data causing the issue:
{
"ads": {
"items": [],
"total": 0,
"page": 1
},
"member": {
"memberId": "649991",
"displayName": "",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066e6775677e7e7e466e6972696263676a2865696b">[email protected]</a>",
"memberStatus": "Y",
"smiId": "7aa205ea-7aa2-7aa2-7aa2-7aa205eac183",
"facebookId": "100001412016857",
"registerDate": null,
"emailConfirmed": false,
"smsConfirmed": false,
"facebookConfirmed": false,
"lineId": null,
"wechatId": null,
"displayImage": "",
"firstName": "Hasadin",
"lastName": "Pankran",
"telephone": "",
"mobile": null,
"listingCount": {
"online": 0,
"edit": 0,
"waiting": 0
},
"phones": null
}
}