使用RestSharp反序列化JSON数组

时间:2016-02-14 00:25:11

标签: c# arrays json deserialization restsharp

我有超出我控制范围的API调用返回以下JSON对象:

{
  "GetBusinessGroupsRestResult": [
  {
    "BgId": 1,
    "NodeLevel": 1,
    "BusinessDescription": "Business Groups",
    "BusinessName": "Root",
    "nodegroupid": 2,
    "nodegroupname": "Root",
    "nodegroupdesc": "Root",
    "sorkkey": "Root",
    "Marked": 2
  },
  {
    "BgId": 2,
    "NodeLevel": 2,
    "BusinessDescription": "Business A",
    "BusinessName": "Business A",
    "ParentID": 1,
    "nodegroupid": 3,
    "nodegroupname": "Business A",
    "nodegroupdesc": "Business A",
    "sorkkey": "Business A",
    "Marked": 2
  },
  ...
 ]
}

我创建了以下类结构,试图将其反序列化为C#对象实例:

public class GetBusinessGroupsRestResult
{
    public List<BusinessGroup> BusinessGroups { get; set; }
}

public class BusinessGroup
{
    [DeserializeAs(Name = "BgId")]
    public int BusinessGroupId { get; set; }

    public int NodeLevel { get; set; }
    public string BusinessDescription { get; set; }
    public string BusinessName { get; set; }

    [DeserializeAs(Name = "ParentID")]
    public int ParentId { get; set; }

    [DeserializeAs(Name = "nodegroupid")]
    public int NodeGroupId { get; set; }

    [DeserializeAs(Name = "nodegroupname")]
    public string NodeGroupName { get; set; }

    [DeserializeAs(Name = "sorkkey")]
    public string SortKey { get; set; }

    public int Marked { get; set; }
}

我正在尝试使用RestSharp对其进行反序列化,将RootElement指定为GetBusinessGroupsRestResult。使用RestSharp&#39; IRestResponse<T> Execute<T>(RestRequest)尝试反序列化时,我收到以下错误:

System.InvalidCastException: Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.

我设法反序排列此API之前没有返回列表的响应,例如:

{
  "AcquireSecurityTokenRestResult": {
    "SecurityToken": "SECURITY-TOKEN",
    "SessionLength": 600000,
    "APIVersion": "VERSION"
  }
}

使用以下POCO:

public class AcquireSecurityTokenRestResult
{
    public string SecurityToken { get; set; }
    public int SessionLength { get; set; }
    [DeserializeAs(Name = "APIVersion")]
    public string ApiVersion { get; set; }
}

有人能指出我在正确的方向吗?谢谢!

2 个答案:

答案 0 :(得分:2)

管理以使其工作。 Execute部分:

var client = new RestClient("http://localhost:3000/");
var request = new RestRequest("main", Method.GET);
var response = client.Execute<RootObject>(request);

班级:

public class RootObject
{
    public List<BusinessGroup> GetBusinessGroupsRestResult { get; set; }
}

public class BusinessGroup
{
    [DeserializeAs(Name = "BgId")]
    public int BusinessGroupId { get; set; }

    public int NodeLevel { get; set; }
    public string BusinessDescription { get; set; }
    public string BusinessName { get; set; }

    [DeserializeAs(Name = "ParentID")]
    public int ParentId { get; set; }

    [DeserializeAs(Name = "nodegroupid")]
    public int NodeGroupId { get; set; }

    [DeserializeAs(Name = "nodegroupname")]
    public string NodeGroupName { get; set; }

    [DeserializeAs(Name = "nodegroupdesc")]
    public string NodeGroupDesc { get; set; }

    [DeserializeAs(Name = "sorkkey")]
    public string SortKey { get; set; }

    public int Marked { get; set; }
}

由于主响应是一个包含名为GetBusinessGroupsRestResult的数组的对象,因此您也需要在根对象中使用它。

您基本上可以通过在DeserializeAs类的BusinessGroups媒体资源中添加GetBusinessGroupsRestResult属性来修复您的示例:

public class GetBusinessGroupsRestResult
{
    [DeserializeAs(Name = "GetBusinessGroupsRestResult")]
    public List<BusinessGroup> BusinessGroups { get; set; }
}

答案 1 :(得分:1)

不仅仅是一个答案,建议。而不是使用RESTSharp为什么不使用Json.NET?

我遇到了RESTSharp的问题,它在Json.NET中运行得非常好。