0

I am working with box api, and trying to parse json object to a class.
This is the json:

{
  "type":"folder",
  "id":"0",
  "sequence_id":null,
  "etag":null,
  "name":"All Files",
  "created_at":null,
  "modified_at":null,
  "description":"",
  "size":9049537,
  "path_collection":
  {
    "total_count":0,"entries":[]
  },
  "created_by":
  {
    "type":"user","id":"","name":"","login":""
  },
  "modified_by":
  {
    "type":"user",
    "id":"111",
    "name":"a a",
    "login":"[email protected]"
  },
  "trashed_at":null,
  "purged_at":null,
  "content_created_at":null,
  "content_modified_at":null,
  "owned_by":
  {
    "type":"user",
    "id":"111",
    "name":"a a",   
    "login":"[email protected]"   
  },    
  "shared_link":null,
  "folder_upload_email":null,
  "parent":null,
  "item_status":"active",
  "item_collection":
  {
    "total_count":4,
    "entries":
    [
      {
        "type":"file",
        "id":"22887167395",
        "sequence_id":"0",
        "etag":"0",
        "sha1":"883c99863eefc0f46b3d34915cc4d97a6008fabf",
        "name":"13.ppt"
      },
      {
        "type":"file",
        "id":"22887169687",
        "sequence_id":"0",
        "etag":"0",
        "sha1":"a345fd68b1c90a3678a3e746e0e5343693d8a022",
        "name":"6.docx"
      }
    ],
    "offset":0,
    "limit":100,
    "order":
    [
      {
        "by":"type",
        "direction":"ASC"
      },
      {
        "by":"name",
        "direction":"ASC"
      }
    ]
  }
}

Basically, this is the root folder (in that case) that contains two files:
13.ppt
6.docx
I created a class:

[JsonObject(MemberSerialization.OptIn)]
public class BoxFile
{
    [JsonProperty(PropertyName = "type")]
    public string Type { get; internal set; }

    [JsonProperty(PropertyName = "id")]
    public string Id { get; internal set; }

    [JsonProperty(PropertyName = "sequence_id")]
    public string SequenceId { get; internal set; }

    [JsonProperty(PropertyName = "etag")]
    public string Etag { get; internal set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; internal set; }

    [JsonProperty(PropertyName = "created_at")]
    public string CreatedAt { get; internal set; }

    [JsonProperty(PropertyName = "modified_at")]
    public string ModifiedAt { get; internal set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; internal set; }

    [JsonProperty(PropertyName = "size")]
    public long Size { get; internal set; }

    [JsonProperty(PropertyName = "item_collection")]
    public IEnumerable<BoxFile> ItemCollection { get; internal set; }
}  

But the "item_collection" part is not working.. it gives me an error..

How do I get a list of subfiles inside "item_collection"?

I use it by:

    private T ParseJson<T>(string json) where T : class, new()
    {
            JObject jobject = JObject.Parse(json);
            return JsonConvert.DeserializeObject<T>(jobject.ToString());
    }  

And:

BoxFile parsed = ParseJson<BoxFile>(json);
2
  • Specify a language tag please Commented Nov 24, 2014 at 10:27
  • The thing about errors is that they often contain useful information for debugging. So never say "It gives me an error". Instead describe the error, copy and paste the words. Tell us if its a compile error or a runtime error, etc. Commented Nov 24, 2014 at 11:53

1 Answer 1

1

You are getting an error because your class structure does not match your JSON. Specifically, in the JSON, the item_collection property is an object, not a list. That JSON object has two properties, total_count and entries, the latter of which contains the actual list of files. To handle this, you need to define another class:

public class ItemCollection
{
    [JsonProperty(PropertyName = "entries")]
    public IEnumerable<BoxFile> Entries { get; internal set; }
}

and then change the ItemCollection property in your BoxFile class to use this new class:

    [JsonProperty(PropertyName = "item_collection")]
    public ItemCollection ItemCollection { get; internal set; }

You can then access the list of files like this:

BoxFile parsed = ParseJson<BoxFile>(json);

foreach (BoxFile file in parsed.ItemCollection.Entries)
{
    Console.WriteLine(file.Name);
}

Here is a working demo: https://dotnetfiddle.net/DB9Coc

As an aside, you can simplify your ParseJson method to one line. There is no need to parse the JSON to an JObject, turn it back into JSON and then parse it again.

private T ParseJson<T>(string json) where T : class, new()
{
    return JsonConvert.DeserializeObject<T>(json);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.