[英]Using RestSharp to deserialise JSON array
I have the following JSON object being returned from an API call beyond my control: 我从我的控件之外的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
},
...
]
}
I have created the following class structure in an attempt to deserialise it into C# object instances: 我创建了以下类结构,试图将其反序列化为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; }
}
I am trying to deserialise it using RestSharp , specifying the RootElement as GetBusinessGroupsRestResult
. 我试图使用RestSharp对其进行反序列化,将RootElement指定为
GetBusinessGroupsRestResult
。 When attempting the deserialisation using RestSharp's IRestResponse<T> Execute<T>(RestRequest)
, I recieve the following error: 使用RestSharp的
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]'.
I've managed to deserialise responses from this API that don't return lists before, for example: 我已设法反序列化此API之前未返回列表的响应,例如:
{
"AcquireSecurityTokenRestResult": {
"SecurityToken": "SECURITY-TOKEN",
"SessionLength": 600000,
"APIVersion": "VERSION"
}
}
With the following POCO: 使用以下POCO:
public class AcquireSecurityTokenRestResult
{
public string SecurityToken { get; set; }
public int SessionLength { get; set; }
[DeserializeAs(Name = "APIVersion")]
public string ApiVersion { get; set; }
}
Can anyone point me in the right direction here? 任何人都能指出我在正确的方向吗? Thank you!
谢谢!
Managed to make it work. 管理使其工作。 The
Execute
part: Execute
部分:
var client = new RestClient("http://localhost:3000/");
var request = new RestRequest("main", Method.GET);
var response = client.Execute<RootObject>(request);
And the classes: 和班级:
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; }
}
Since the main response is an object which contains an array called GetBusinessGroupsRestResult
, you need that in your root object as well. 由于主响应是一个包含名为
GetBusinessGroupsRestResult
的数组的对象,因此您也需要在根对象中使用它。
You can basically fix your example by adding a DeserializeAs
attribute to the BusinessGroups
property in your GetBusinessGroupsRestResult
class: 您可以通过在
GetBusinessGroupsRestResult
类的BusinessGroups
属性中添加DeserializeAs
属性来基本修复您的示例:
public class GetBusinessGroupsRestResult
{
[DeserializeAs(Name = "GetBusinessGroupsRestResult")]
public List<BusinessGroup> BusinessGroups { get; set; }
}
More than an answer, piece of advice. 不仅仅是一个答案,一条建议。 Instead of using RESTSharp why don't you use Json.NET?
而不是使用RESTSharp为什么不使用Json.NET?
I ran into issues with RESTSharp that works wonderfully in Json.NET. 我遇到了RESTSharp的问题,它在Json.NET中运行得非常好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.