I'm working with OneDrive and need to get information about a folders contents back from the server. This is the type of data I am working with:

{


"data": [
      {
         "id": "folder.xxxx", 
         "from": {
            "name": "john doe", 
            "id": "xxxx"
         }, 
         "name": "Files that are in a folder", 
         "description": "", 
         "parent_id": "folder.xxxx", 
         "size": 0, 
         "upload_location": "https://apis.live.net/v5.0/folder.xxxx/files/", 
         "comments_count": 0, 
         "comments_enabled": false, 
         "is_embeddable": true, 
         "count": 0, 
         "link": "xxxx", 
         "type": "folder", 
         "shared_with": {
            "access": "Just me"
         }, 
         "created_time": "2014-03-06T18:48:16+0000", 
         "updated_time": "2014-03-06T18:48:16+0000", 
         "client_updated_time": "2014-03-06T18:48:16+0000"
      }, {
         "id": "file.xxxx", 
         (same as above for rest of data structure)
      }, {
         "id": "file.xxxx", 
         (Same as above for rest of data structure)
      }
   ]
}

When doing a different request to the server you get back just the ("id" : "folder.xxx") info chunk and I was able to process that data using a class that looks like this:

[DataContract]
    public class ResponseFolder
    {
        [DataMember(Name = "id")]
        public string id { get; set; }
        [DataMember(Name = "from")]
        public from from { get; set; }
        [DataMember(Name = "name")]
        public string name { get; set; }
        //etc.

And handling the entries like "from" with similar structures:

[DataContract]
    public class from
    {
        public string name { get; set; }
        public string id { get; set; }
    }

I thought I could do the same for the data request at the top and so have this class which is not working for me:

[DataContract]
    public class FolderRequest
    {
        [DataMember(Name = "data")]
        public ResponseFolder data { get; set; }
    }

And I try to use it on this line:

FolderRequest = jss.Deserialize<FolderRequest>(json);

But FolderRequest is null after that. I have also tried doing

jss.Deserialize<List<ResponseFolder>>(json);

after googling around how to handle arrays in json but that did not work either.

Any help would be appreciated!

有帮助吗?

解决方案

Your complete model is

public class From
{
    public string name { get; set; }
    public string id { get; set; }
}

public class SharedWith
{
    public string access { get; set; }
}

public class ResponseFolder
{
    public string id { get; set; }
    public From from { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string parent_id { get; set; }
    public int size { get; set; }
    public string upload_location { get; set; }
    public int comments_count { get; set; }
    public bool comments_enabled { get; set; }
    public bool is_embeddable { get; set; }
    public int count { get; set; }
    public string link { get; set; }
    public string type { get; set; }
    public SharedWith shared_with { get; set; }
    public string created_time { get; set; }
    public string updated_time { get; set; }
    public string client_updated_time { get; set; }
}

public class FolderRequest
{
    public List<ResponseFolder> data { get; set; }
}

and you should serialize as

var obj = new JavaScriptSerializer().Deserialize<FolderRequest>(DATA);

其他提示

Looks like data needs to be an array:

[DataContract]
public class FolderRequest
{
    [DataMember(Name = "data")]
    public ResponseFolder[] data { get; set; }
}

If you are ok with use Json.Net (http://james.newtonking.com/json), try this:

public class From
{
    public string Name { get; set; }

    public string Id { get; set; }
}

public class SharedWith
{
    [JsonProperty(PropertyName = "access")]
    public string AccessName { get; set; }
}

public class ResponseFolder
{
    public string Id { get; set; }

    public From From { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [JsonProperty(PropertyName = "shared_with")]
    public SharedWith SharedWith { get; set; }
}

public class RootObject
{
    public List<ResponseFolder> data { get; set; }
}

And then your deserialization:

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

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top