gpt4 book ai didi

c# - RestSharp JsonDeserializer 列表对象

转载 作者:行者123 更新时间:2023-11-30 22:57:50 25 4
gpt4 key购买 nike

我想使用 RestSharp 反序列化反序列化带有数组的 JSON。

public class details
{
public string id { get; set; }
public string tran_id { get; set; }
public string tran_type { get; set; }
public string tran_status { get; set; }
public string expiry_date_time { get; set; }
public string number { get; set; }
}

我的 JSON 如下:

{
"details": [
{
"id": "ebca66079b44",
"tran_id": "c9b1bce025f5",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:33:04+00:00",
"number": "12345678ABC"
},
{
"id": "ebca66079b44",7c2445c8-a5ba-4ad2-a38e-3ea682c60edf",
"tran_id": "3ea682c60edf",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:26:28+00:00",
"number": "22345678ABC"
},
{
"id": "ebca66079b44",
"tran_id": "e40c45817985",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:26:06+00:00",
"number": "32345678ABC"
}
]
}

我的代码是:

IRestResponse response = client.Execute(request);
//Deserialize Json
return new JsonDeserializer().Deserialize<List<details>>(response);

我能够获取“详细信息”,但不能获取对象内的列表。

最佳答案

你需要用一个对象来包含你的JSON数组数据,因为最外层是一个对象而不是数组。

public class JsonModel
{
public List<Detail> details { get; set; }
}


public class Detail
{
public string id { get; set; }
public string tran_id { get; set; }
public string tran_type { get; set; }
public string tran_status { get; set; }
public string expiry_date_time { get; set; }
public string number { get; set; }
}

像这样使用。

new JsonDeserializer().Deserialize<JsonModel>(response);

注意

有一个 json 数据可能会从 "ebca66079b44",7c2445c8-a5ba-4ad2-a38e-3ea682c60edf", 数据中抛出错误。

有两种方法可以轻松创建模型。

  • 您可以在 Visual Studio 中使用 Web Essentials,使用“编辑”>“选择性粘贴”>“将 JSON 作为类粘贴”,您可以更轻松地了解 Json 与模型之间的关系。

  • 如果您不能使用 Web Essentials,您可以使用 https://app.quicktype.io/?l=csharp在线 JSON 到模型类。

您可以尝试使用那些模型来承载您的 JSON 格式。

关于c# - RestSharp JsonDeserializer 列表对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53371641/

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