反序列化Onedrive REST数据

时间:2014-03-14 20:16:28

标签: c# json onedrive

我正在使用OneDrive,需要从服务器获取有关文件夹内容的信息。这是我正在使用的数据类型:

{


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

当对服务器做出不同的请求时,你只能回到(​​" id":" folder.xxx")信息块,我能够使用类处理该数据看起来像这样:

[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.

处理"来自"结构相似:

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

我认为我可以对顶部的数据请求做同样的事情,所以这个课程对我不起作用:

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

我试着在这一行上使用它:

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

但是之后FolderRequest为null。我也尝试过做

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

在谷歌搜索如何处理json中的数组但是也没有用。

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

您的完整型号

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

你应该序列化为

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

答案 1 :(得分:0)

看起来数据需要是一个数组:

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

答案 2 :(得分:0)

如果你可以使用Json.Net(http://james.newtonking.com/json),试试这个:

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

然后你的反序列化:

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

希望这有帮助。