gpt4 book ai didi

c# - 使用 RestSharp 反序列化 JSON 数组

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:10 25 4
gpt4 key购买 nike

我有以下 JSON 对象从我无法控制的 API 调用中返回:

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

谁能给我指明正确的方向?谢谢!

最佳答案

设法让它发挥作用。 执行部分:

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 属性添加到 GetBusinessGroupsRestResult 类中的 BusinessGroups 属性来修复您的示例:

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

关于c# - 使用 RestSharp 反序列化 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com