使用RestSharp反序列化Json以获取属性

时间:2014-08-07 20:32:35

标签: c# json restsharp

我在使用RestSharp从以下JSON结果中提取“Id”属性时遇到问题: **这是JSON *

{

  "odata.metadata":"Api/v1/$metadata#Folders","odata.count":"1","value":
  [

    {

      "odata.id":"/Api/v1/Folders('c8644e97b4ca4353b5bd74a0cc37588214')","Presentations@odata.navigationLinkUrl":"/Api/v1/Folders('c8644e97b4ca4353b5bd74a0cc37588214')/Presentations","#UpdatePermissions":
      {

        "target":"Api/v1/Folders('c8644e97b4ca4353b5bd74a0cc37588214')/UpdatePermissions"

      }
      ,"Id":"c8644e97b4ca4353b5bd74a0cc37588214","Name":"2013-AAFGSW","Owner":"John Doe","Description":"EPIDEM 150.03, Summer 2013","CreationDate":"2014-06-09T22:00:43","LastModified":"2014-06-09T22:00:43","ParentFolderId":"2f5469c7bdf641878c8baf2988ceeb9a14","Recycled":false,"Type":"Folder"

    }

  ]

}

以下是我正在使用的课程:

public class Value
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
    public string Description { get; set; }
    public string CreationDate { get; set; }
    public string LastModified { get; set; }
    public string ParentFolderId { get; set; }
    public bool Recycled { get; set; }
    public string Type { get; set; }
}

public class FolderRoot <T>
{

    public List<Value> value { get; set; }
}

以下是请求代码:

                var BuildFolderIdRequest = new RestRequest(Method.GET);
                BuildFolderIdRequest.Resource = string.Format("Folders?$filter=Name eq '{0}'",x.event_locator);
                BuildFolderIdRequest.RequestFormat = DataFormat.Json;
                BuildFolderIdRequest.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
                BuildFolderIdRequest.AddHeader("apikey", ApiKey);
    var Fold = MediasiteClient.Execute<FolderRoot<Value>>(BuildFolderIdRequest);
                Console.WriteLine("The folder content is {0}", Fold.Content);
                Console.WriteLine("The folder Id is {0}", Fold.Data.????);
                Console.ReadKey();

问题是试图提取“Id” - 因为我不能用“Fold.Data.Id”来做,而我只是将“Fold.Data.value”作为我唯一的选择... < / p>

2 个答案:

答案 0 :(得分:1)

看起来您正在使用MediaSite的新v7 REST API。在执行请求之前尝试以下操作:

BuildFolderIdRequest.RootElement = "value";

如果您查看已使用Fold.Content收到的原始JSON结果,您应该会看到所需的结果位于名为value的节点下。如果没有该映射,反序列化可能不起作用。祝你好运。

答案 1 :(得分:0)

我认为您需要先选择列表中的元素,然后才能从中获取ID, 像

        Fold.Data[0].Id

这有用吗?我不太熟悉反序列化,但这就是我的样子。

“Folder.Data.value”的类型是否为列表?