我有一个JSON
的响应,我试图转换成Datatable C#
使用JsonConvert.DeserializeObject
,但我得到错误,因为
在完成对象格式化后,在JSON字符串中发现其他文本
下面是我的JSON响应
{
"Status": "Success",
"ErrorCode": "00",
"ErrMsg": "",
"BuildingRJID": "CHNNBD0862314",
"airFiberFeasibility": "P",
"fiberFeasibility": "P",
"PMPFeasibility": "V",
"Coverage_5G": "No",
"geoCoordinates": "null,null",
"FloorWiseStatus": [],
"address": {
"buildingId": "CHNNBD0862314",
"jioCenterId": "null",
"jioPointId": "",
"JCSAPId": "null",
"JPSAPId": "I-TN-CHNN-UJP-0035",
"bldngStatus": "NR_RFS",
"fsaId": "",
"fsaStatus": null,
"csaDesign": ""
}
}
另请参阅我将代码转换为Datatable失败的地方
try
{
strInputJSON_CheckRFS_Status = (new JavaScriptSerializer()).Serialize(inputCheckRFS_ID);
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
string jsonData = client.UploadString(apiUrl_CheckBldgRFS_Status, strInputJSON_CheckRFS_Status);
DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsonData, (typeof(DataTable))); // here it's failing
}
catch (Exception ex)
{
throw ex;
}
1条答案
按热度按时间zsohkypk1#
问题是JSON对象无法转换为
DataTable
。除非你的数据/JSON是一个数组。1.将
jsonData
读取为JObject
。1.将
JObject
示例添加到JArray
中。1.迭代
JArray
作为投影。1.将
JArray
示例转换为DataTable
。由于您希望返回数据并将其显示在HTML表中,因此没有必要将其作为
DataTable
(这是一种旧技术)返回。将数据返回为JArray
或List<T>
会更容易。或