nix
nix

Reputation: 3302

Why is my JavaScriptSerializer().Deserialize() is not working?

I get this JSON from Box API call:

   {"total_count":4,
    "entries":[
        {"type":"folder","id":"3102883693","sequence_id":"0","etag":"0","name":"Deployments"},            
        {"type":"folder","id":"3460455852","sequence_id":"0","etag":"0","name":"MARKETING"},
        {"type":"folder","id":"2535410485","sequence_id":"1","etag":"1","name":"Plans"},
        {"type":"folder","id":"3132381455","sequence_id":"0","etag":"0","name":"Projects"}, 
        ],
    "offset":0,
    "limit":100,
    "order":[
        {"by":"type","direction":"ASC"},
        {"by":"name","direction":"ASC"}
        ]
    }

I tried this to get it into class but I cant get my list:

var folders = new JavaScriptSerializer().Deserialize<List<FolderItems>>(response.Content);

Here are my classes:

   public class FolderItems
   {
       public int total_count { get; set; }
       public List<Entry> entries { get; set; }
       public int offset { get; set; }
       public int limit { get; set; }
       public List<Order> order { get; set; }
   }
    public class Entry
    {
        public string type { get; set; }
        public int id { get; set; }
        public int sequence_id { get; set; }
        public string etag { get; set; }
        public string name { get; set; }
    }

public class Order
{
    public string by { get; set; }
    public string direction { get; set; }
}

Upvotes: 0

Views: 1290

Answers (1)

David L
David L

Reputation: 33833

Based on your JSON, you have a single outer object, not a list.

var folder = new JavaScriptSerializer().Deserialize<FolderItems>(response.Content);

You should be deserializing into a single FolderItems object with a list of entries on that object.

Upvotes: 3

Related Questions