我从我的服务器接收了以下JSON对象
"[{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"National Savings Bank \",\"Bank_Name\":\"National Savings Bank \",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"NDB Bank\",\"Bank_Name\":\"NDB Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Peoples Merchant Bank\",\"Bank_Name\":\"Peoples Merchant Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"DFCC Vardhana Bank\",\"Bank_Name\":\"DFCC Vardhana Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Mercantile Merchant Bank\",\"Bank_Name\":\"Mercantile Merchant Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Union Commercial Bank\",\"Bank_Name\":\"Union Commercial Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Deutsche Bank\",\"Bank_Name\":\"Deutsche Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Hatton National Bank PLC\",\"Bank_Name\":\"Hatton National Bank PLC\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Seylan Merchant Bank\",\"Bank_Name\":\"Seylan Merchant Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Nations Trust Bank\",\"Bank_Name\":\"Nations Trust Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"HSBC\",\"Bank_Name\":\"HSBC\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Standard Chartered Bank\",\"Bank_Name\":\"Standard Chartered Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"State Bank of India\",\"Bank_Name\":\"State Bank of India\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Central Bank of Sri Lanka\",\"Bank_Name\":\"Central Bank of Sri Lanka\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Peoples Bank\",\"Bank_Name\":\"Peoples Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Pan Asia Banking Corporation PLC\",\"Bank_Name\":\"Pan Asia Banking Corporation PLC\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Central Bank\",\"Bank_Name\":\"Central Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Commercial Bank of Ceylon\",\"Bank_Name\":\"Commercial Bank of Ceylon\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Amana Bank\",\"Bank_Name\":\"Amana Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Other\",\"Bank_Name\":\"Other\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Bank of Ceylon\",\"Bank_Name\":\"Bank of Ceylon\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"ICICI Bank\",\"Bank_Name\":\"ICICI Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Foreign Banks\",\"Bank_Name\":\"Foreign Banks\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"MBSL Savings Bank\",\"Bank_Name\":\"MBSL Savings Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Citibank\",\"Bank_Name\":\"Citibank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Sampath Bank PLC\",\"Bank_Name\":\"Sampath Bank PLC\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Merchant Bank of Sri Lanka\",\"Bank_Name\":\"Merchant Bank of Sri Lanka\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"DFCC Bank\",\"Bank_Name\":\"DFCC Bank\",\"Status\":0},{\"Sync_Time\":\"\\/Date(1447935488033)\\/\",\"Bank_Code\":\"Seylan Bank PLC\",\"Bank_Name\":\"Seylan Bank PLC\",\"Status\":0}]"
我使用以下方法将其转换为C#DataTable
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd() ;
var table = JsonConvert.DeserializeObject<DataTable>(result);
}
但它会出现以下错误;
类型&#39; Newtonsoft.Json.JsonSerializationException&#39;的例外情况 发生在Newtonsoft.Json.dll但未在用户代码中处理
其他信息:读取DataTable时出现意外的JSON令牌。 预期的StartArray,得到了String。路径&#39;&#39;,第1行,第3784位。
有人可以解释原因吗?
答案 0 :(得分:0)
首先,使用对象结构创建一个类:
[JsonObject]
public class BankData
{
[JsonProperty(PropertyName = "Sync_Time")]
public string Sync_Time { get; set; }
[JsonProperty(PropertyName = "Bank_Code")]
public string Bank_Code { get; set; }
[JsonProperty(PropertyName = "Bank_Name")]
public string Bank_Name { get; set; }
[JsonProperty(PropertyName = "Status")]
public int Status { get; set; }
}
然后创建一个解析json并将其转换为数组的方法:
var jsonResponse = .....get json from server...
var res = JsonConvert.DeserializeObject<BankData[]>(jsonResponse);
// res is your array of BankData.... you can use it here...