Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Json to List<object> with .NET

Tags:

json

c#

.net

After days to try to convert Json to List of objects, I'm here. I have a REST API that return the Json string:

{
   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
      }
   ]"
} 

I know that this string comes from a DataTable:

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

I created two class: one that describe the object model and another that get the collection. But when trying to

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

I obtain Could not cast or convert from System.String to System.Collections.Generic.List`1[Volume].

What is wrong?

Volume Class:

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; }
}

The json string received is: "\" [{\\"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\\":true,\\"VolumeType\\":2,\\"ClassId\\":223108653,\\"Code\\":\\"VOL00056\\",\\"Status\\":1} ]\""

It seems to be a not valid json...

like image 620
Old-fashioned-dev Avatar asked May 21 '26 14:05

Old-fashioned-dev


2 Answers

Given that your json is not valid and given that the following is:

{
    "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
    }]
}

using the following classes that I generated, this works:

    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: this site generated the classes from the JSON: json2csharp

like image 155
Ric Avatar answered May 24 '26 04:05

Ric


Your GetItemsListResult is a string, not an array. Notice the double quotes:

GetItemsListResult: "[

So, ideally you want to fix your json to be a real array.

If you don't have control over the json, then as a poor alternative you could recursively parse the strings to extract the array.

like image 27
Buh Buh Avatar answered May 24 '26 04:05

Buh Buh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!