【发布时间】:2019-10-31 04:39:29
【问题描述】:
我正在为 C#.NET 创建一个 ActiveCampaign V1.1 包装器,并创建一个使用 (https://www.activecampaign.com/api/example.php?call=list_list) 列出联系人列表的类
JsonConvert.DeserializeObject<BasicListResponse>(JSON) 之后返回的响应对象确实填充了非列表属性,但 JSON 的列表部分未转换。
我尝试了以下BasicListResponse 实现:
public class Result
{
[JsonProperty("result_code")]
public int ResultCode { get; set; }
[JsonProperty("result_message")]
public string ResultMessage { get; set; }
[JsonProperty("result_output")]
public string ResultOutput { get; set; }
}
public class BasicListResponse : Result
{
public Dictionary<string, BasicList> list { get; set; }
}
public class BasicList
{
[JsonProperty("id")]
public string Id { get; set; }
[Required]
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("cdate")]
public DateTime CreatedOn { get; set; }
[JsonProperty("private")]
public bool Private { get; set; }
[JsonProperty("userid")]
public string UserId { get; set; }
[JsonProperty("subscriber_count")]
public int SubscriberCount { get; set; }
}
我也尝试过使用json2csharp 或 VS > 选择性粘贴,但输出有编号的类而不是对象列表。
JSON 响应如下:
{
"0": {
"id": "1",
"name": "xxxxx xxxxxx xxxxxxx",
"cdate": "2019-10-27 22:43:23",
"private": "0",
"userid": "1",
"subscriber_count": 1
},
"1": {
"id": "2",
"name": "yyyyy yyyyy yyyyy",
"cdate": "2019-10-27 22:44:03",
"private": "0",
"userid": "1",
"subscriber_count": 0
},
"result_code": 1,
"result_message": "Success: Something is returned",
"result_output": "json"
}
我在包装器中提取了Dictionary<T,T>,但列表始终为 NULL。
如果我重新序列化BasicListResponse,结果如下
{ "list": null, "result_code": 1, "result_message": "Success: Something is returned", "result_output": "json" }
我希望 BasicListResponse 用作根对象,其中包含具有三个字符串 result_* 属性的列表/数组。
感谢您为解决此问题提供的任何帮助。
【问题讨论】:
-
JSON 对象不包含
list。也许你需要this question? -
app.quicktype.io/#l=cs&r=json2csharp 试试这个以获得与您的 JSON 匹配的正确类结构
-
也许这有帮助:dotnetfiddle.net/eSLWpe
-
@john - 这个问题也很有帮助。 @rahul-sharma - 您的解决方案有效,但我选择了 @brian-rodgers,因为我倾向于直接转换为
List<T>而不是dynamic
标签: c# .net json json.net deserialization