gpt4 book ai didi

c# - 使用 restsharp 反序列化 JSON

转载 作者:太空狗 更新时间:2023-10-29 22:32:08 24 4
gpt4 key购买 nike

我正在尝试反序列化来自 Harvest 的数据,但它失败了(没有错误):https://github.com/harvesthq/api#api-json

返回的数据如下所示:

已更新(请参阅底部以获取完整的 JSON 响应)

当我运行下面的代码时,输​​出是一个包含 x 个帖子的列表,其中每个帖子包含一个 id = 0

是否有设置或我遗漏的东西让它忽略/解析周围的 []?

[DeserializeAs(Name = "project")]
public class Project
{
public int id { get; set; }

//public string name { get; set; }

//[DeserializeAs(Name = "created-at")]
//public DateTime CreatedAt { get; set; }
}

// The following is the methods to request for testing

public List<Project> GetProjects()
{
var request = new RestRequest("projects", Method.GET);
request.RequestFormat = DataFormat.Json;
return Execute<List<Project>>(request);
}

private T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = BaseUrl;
client.Authenticator = new HttpBasicAuthenticator(_username, _password);
var response = client.Execute<T>(request);

if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var exception = new ApplicationException(message, response.ErrorException);
throw exception;
}
return response.Data;
}

从 Harvest 返回的数据:

[
{
"project": {
"id": 123456,
"client_id": 219854,
"name": "Test proj 1",
"code": "",
"active": false,
"billable": true,
"bill_by": "Tasks",
"cost_budget": null,
"cost_budget_include_expenses": false,
"hourly_rate": null,
"budget": 8,
"budget_by": "project",
"notify_when_over_budget": false,
"over_budget_notification_percentage": 80,
"over_budget_notified_at": null,
"show_budget_to_all": false,
"created_at": "2014-04-03T09:49:00Z",
"updated_at": "2014-07-02T11:45:07Z",
"estimate": 8,
"estimate_by": "project",
"hint_earliest_record_at": "2014-04-03",
"hint_latest_record_at": "2014-04-03",
"notes": ""
}
},
{
"project": {
"id": 234567,
"client_id": 686547,
"name": "Test porj 2",
"code": "",
"active": true,
"billable": true,
"bill_by": "Tasks",
"cost_budget": null,
"cost_budget_include_expenses": false,
"hourly_rate": null,
"budget": 8,
"budget_by": "project",
"notify_when_over_budget": false,
"over_budget_notification_percentage": 80,
"over_budget_notified_at": null,
"show_budget_to_all": false,
"created_at": "2014-04-03T09:48:28Z",
"updated_at": "2014-04-15T20:47:29Z",
"estimate": 8,
"estimate_by": "project",
"hint_earliest_record_at": "2014-04-03",
"hint_latest_record_at": "2014-04-03",
"notes": ""
}
},
{
"project": {
"id": 345678,
"client_id": 987456,
"name": "Test proj 3",
"code": "",
"active": false,
"billable": true,
"bill_by": "Project",
"cost_budget": null,
"cost_budget_include_expenses": false,
"hourly_rate": null,
"budget": 8,
"budget_by": "project",
"notify_when_over_budget": false,
"over_budget_notification_percentage": 80,
"over_budget_notified_at": null,
"show_budget_to_all": false,
"created_at": "2013-04-26T13:21:35Z",
"updated_at": "2014-03-30T18:05:24Z",
"estimate": 8,
"estimate_by": "project",
"hint_earliest_record_at": "2013-04-26",
"hint_latest_record_at": "2013-12-04",
"notes": "Scriblings from meeting ..."
}
}
]

最佳答案

您误解了 JSON 响应。

[
{
"project": {
"id": 123456
}
},
{
"project": {
"id": 234567
}
}
]

这是一个包含对象的数组,其中包含一个项目。我不熟悉 RestSharp,但应该这样做:

public class SomeType
{
public Project project { get; set; }
}
return Execute<List<SomeType>>(request);

尽管根据 the documentation /projects 应该返回一个项目数组,但您可能想与项目维护者取得联系。

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

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