将Json转换为List与.net

本文关键字:object net List Json 转换 | 更新日期: 2023-09-27 18:11:39

经过几天尝试将Json转换为对象列表,我在这里。我有一个REST API返回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
      }
   ]"
} 

我知道这个字符串来自一个数据表:

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

我创建了两个类:一个描述对象模型,另一个获取集合。但是当试图

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

我获得无法从系统强制转换或转换。String to System.Collections.Generic.List ' 1[Volume].

怎么了?

卷类:

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 ' ' ": ' '"邮件esterne ' '",' ' " CloseDate ' ' ": 20161001, ' '"CloseTime ' '":212512年,' ' " IsFull ' ' ":真的,' ' " VolumeType ' ' ": 2 ' ' " ClassId ' ' ": 111760419, ' ' "代码' ' ":' ' " VOL00057 ' ' ", ' ' ' '"状态":1},{' '"FreeSpace ' '":0.0,' ' " TotalSpace ' ' ": 671088640.0 ' '"FillPercentage ' '":99年,' ' " ClassDescription ' ' ": ' ' " Corrispondenza没有邮件' ' ",' ' " CloseDate ' ' ": 20160926, ' '"CloseTime ' '":112456年,' ' " IsFull ' ' ":真的,' ' " VolumeType ' ' ": 2 ' ' " ClassId ' ' ": 223108653, ' ' "代码' ' ":' ' " VOL00056 ' ' ", ' ' ' '"状态":1}] ' "

将Json转换为List<object>与.net

假设您的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> GetItemsListResult { get; set; }
    }

var res = JsonConvert.DeserializeObject<RootObject>(json);

N。B:本网站生成的类来自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}
]
}
  • 使用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> GetItemsListResult { get; set; }
    }
    
  • with http://www.newtonsoft.com/json help解析json
  • 3.1像这样:

    var jsonObj = JsonConvert.DeserializeObject<RootObject>(jsonString);