使用.NET将Json转换为List <object>

时间:2016-10-04 13:35:04

标签: c# .net json

在几天之后尝试将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<VolumeCollection>(listVolumes);
我得到了     无法从System.String转换或转换为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字符串是: &#34; \&#34; [{\\&#34;自由空间\\&#34;:0.0,\\&#34; TotalSpace \\&#34;:671088640.0,\\&#34; FillPercentage \\&#34;:99, \\&#34; ClassDescription \\&#34;:\\&#34; Email esterne \\&#34;,\\&#34; CloseDate \\&#34;:20161001,\\&#34 ; CloseTime \\&#34;:212512,\\&#34; IsFull \\&#34;:真,\\&#34; VolumeType \\&#34;:2,\\&#34; CLASSID \\&#34;:111760419,\\&#34;代码\\&#34;:\\&#34; VOL00057 \\&#34;,\\&#34;状态\\&#34; :1}, {\\&#34;自由空间\\&#34;:0.0,\\&#34; TotalSpace \\&#34;:671088640.0,\\&#34; FillPercentage \\&#34;:99,\ \&#34; ClassDescription \\&#34;:\\&#34; Corrispondenza没有邮件\\&#34;,\\&#34; CloseDate \\&#34;:20160926,\\&#34 ; CloseTime \\&#34;:112456,\\&#34; IsFull \\&#34;:真,\\&#34; VolumeType \\&#34;:2,\\&#34; CLASSID \\&#34;:223108653,\\&#34;代码\\&#34;:\\&#34; VOL00056 \\&#34;,\\&#34;状态\\&#34; :1} ] \&#34;&#34;

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

3 个答案:

答案 0 :(得分:3)

鉴于您的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:此网站从JSONjson2csharp

生成了这些类

答案 1 :(得分:1)

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

GetItemsListResult: "[

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

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

答案 2 :(得分:1)

  1. 你的json字符串无效:
  2. 1.1有效的json像这样:

    public int[] evenOdd(int[] nums) {
        if (nums.length < 2) return nums;
    
        int[] result = new int[nums.length];
        int even = 0;
        int odd = nums.length - 1;
    
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 2 == 0)
                result[even++] = nums[i];
            else
                result[odd--] = nums[i];
        }
        return result;
    }
    
    1. 使用http://json2csharp.com/构建数据模型
    2. 2.1喜欢这样:

      {
      "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://www.newtonsoft.com/json帮助解析你json
      2. 3.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; }
        }