将JSON转换为数据表时出错

本文关键字:出错 数据表 JSON 转换 | 更新日期: 2023-09-27 17:58:38

我从服务器收到以下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#数据表

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd() ;
    var table = JsonConvert.DeserializeObject<DataTable>(result); 
}

但它给出了以下错误;

"Newtonsoft.Json.JsonSerializationException"类型的异常发生在Newtonsoft.Json.dll中,但未在用户代码中处理

附加信息:读取DataTable时出现意外的JSON令牌。应为StartArray,实际为String。路径",第1行,位置3784。

有人能解释一下原因吗?

将JSON转换为数据表时出错

首先,使用对象结构创建一个类:

  [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...