使用.NET将Json转换为List

经过几天试图将Json转换为对象列表,我就在这里。 我有一个返回Json字符串的REST API:

{ GetItemsListResult:"[ { "code":"VOL00056", "clsID":223108653, "type":2, "status":1, "free":0.0, "total":671088640.0, "perc":99, "descr":"no mailing", "dt":20160926, "tm":112456, "full":1 }, { "code":"VOL00055", "clsID":111760419, "type":2, "status":1, "free":0.0, "total":671088640.0, "perc":99, "descr":"Email", "dt":20160817, "tm":222411, "full":1 } ]" } 

我知道这个字符串来自DataTable:

 String JSONresult = JsonConvert.SerializeObject(ds.Tables[0]); 

我创建了两个类:一个描述对象模型,另一个描述对象模型。 但是在尝试时

 VolumeCollection volumes = Newtonsoft.Json.JsonConvert.DeserializeObject(listVolumes); 

我获取无法转换或从System.String转换为System.Collections.Generic.List`1 [卷]。

怎么了?

卷类:

 public class Volume { public String code { get; set; } public Int32 classId { get; set; } public Byte volumeType { get; set; } public Byte status { get; set; } public float freeSpace { get; set; } public float totalSpace { get; set; } public Int16 fillPercentage { get; set; } public String classDescription { get; set; } public Int32 closeDate { get; set; } public Int32 closeTime { get; set; } public Boolean isFull { get; set; } } 

收到的json字符串是:“\”[{\\“FreeSpace \\”:0.0,\\“TotalSpace \\”:671088640.0,\\“FillPercentage \\”:99,\\“ClassDescription \\”:\ \“Email esterne \\”,\\“CloseDate \\”:20161001,\\“CloseTime \\”:212512,\\“IsFull \\”:true,\\“VolumeType \\”:2,\\ “ClassId \\”:111760419,\\“Code \\”:\\“VOL00057 \\”,\\“Status \\”:1},{\\“FreeSpace \\”:0.0,\\“TotalSpace \\“:671088640.0,\\”FillPercentage \\“:99,\\”ClassDescription \\“:\\”Corrispondenza no mailing \\“,\\”CloseDate \\“:20160926,\\”CloseTime \\ “:112456,\\” IsFull \\ “:真实的,\\” VolumeType \\ “:2,\\” CLASSID \\ “:223108653,\\” 代码\\ “:\\” VOL00056 \\” \\“状态\\”:1}] \“”

这似乎是一个无效的json ……

鉴于您的json无效并且给出以下内容:

 { "GetItemsListResult": [{ "code": "VOL00056", "clsID": 223108653, "type": 2, "status": 1, "free": 0.0, "total": 671088640.0, "perc": 99, "descr": "no mailing", "dt": 20160926, "tm": 112456, "full": 1 }, { "code": "VOL00055", "clsID": 111760419, "type": 2, "status": 1, "free": 0.0, "total": 671088640.0, "perc": 99, "descr": "Email", "dt": 20160817, "tm": 222411, "full": 1 }] } 

使用我生成的以下类,这有效:

  public class GetItemsListResult { public string code { get; set; } public int clsID { get; set; } public int type { get; set; } public int status { get; set; } public double free { get; set; } public double total { get; set; } public int perc { get; set; } public string descr { get; set; } public int dt { get; set; } public int tm { get; set; } public int full { get; set; } } public class RootObject { public List GetItemsListResult { get; set; } } 

var res = JsonConvert.DeserializeObject(json);

注意:这个站点从JSON : json2csharp生成了类

您的GetItemsListResult是一个字符串,而不是一个数组。 请注意双引号:

 GetItemsListResult: "[ 

因此,理想情况下,您希望将json修复为真正的数组。

如果您无法控制json,那么作为一个不好的替代方案,您可以递归地解析字符串以提取数组。

  1. 你有无效的json字符串:

1.1有效的json像这样:

 { "GetItemsListResult":[ {"code":"VOL00056","clsID":223108653,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"no mailing","dt":20160926,"tm":112456,"full":1}, {"code":"VOL00055","clsID":111760419,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"Email","dt":20160817,"tm":222411,"full":1} ] } 
  1. 使用http://json2csharp.com/构建数据模型

2.1像这样:

 public class GetItemsListResult { public string code { get; set; } public int clsID { get; set; } public int type { get; set; } public int status { get; set; } public double free { get; set; } public double total { get; set; } public int perc { get; set; } public string descr { get; set; } public int dt { get; set; } public int tm { get; set; } public int full { get; set; } } public class RootObject { public List GetItemsListResult { get; set; } } 
  1. 与http://www.newtonsoft.com/json帮助解析你json

3.1像这样:

 var jsonObj = JsonConvert.DeserializeObject(jsonString);