[英]Using RestSharp to deserialise JSON array
我從我的控件之外的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的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; }
}
任何人都能指出我在正確的方向嗎? 謝謝!
管理使其工作。 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
的數組的對象,因此您也需要在根對象中使用它。
您可以通過在GetBusinessGroupsRestResult
類的BusinessGroups
屬性中添加DeserializeAs
屬性來基本修復您的示例:
public class GetBusinessGroupsRestResult
{
[DeserializeAs(Name = "GetBusinessGroupsRestResult")]
public List<BusinessGroup> BusinessGroups { get; set; }
}
不僅僅是一個答案,一條建議。 而不是使用RESTSharp為什么不使用Json.NET?
我遇到了RESTSharp的問題,它在Json.NET中運行得非常好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.