【发布时间】:2019-08-04 09:43:58
【问题描述】:
在使用 HttpClient 类将我的 JSON 转换为字符串并使用
反序列化之后var response = Res.Content.ReadAsStringAsync().Result;
data = JsonConvert.DeserializeObject<List<Employee>>(response);
如何使用下面的模型将我在控制器中收到的数据从调用传递到视图?
public class RuleType
{
public int Id { get; set; }
public string Description { get; set; }
public bool Inactive { get; set; }
}
public class RuleCategory
{
public int Id { get; set; }
public string Description { get; set; }
public bool Inactive { get; set; }
}
public class Employee
{
public string Description { get; set; }
public object EndDateTime { get; set; }
public int Id { get; set; }
public bool Inactive { get; set; }
public int RuleAction { get; set; }
public DateTime StartDateTime { get; set; }
public RuleType RuleType { get; set; }
public RuleCategory RuleCategory { get; set; }
}
这是调用中的一个对象
[
{
"Description": "Test Description",
"EndDateTime": null,
"Id": 1,
"Inactive": false,
"RuleAction": -2,
"StartDateTime": "2017-01-06T14:58:58Z",
"RuleType": {
"Id": 6,
"Description": "Test Description",
"Inactive": false
},
"RuleCategory": {
"Id": 1,
"Description": "Description",
"Inactive": false
}
}
]
【问题讨论】:
-
该 JSON 字符串包含
RootObject实例的数组,而不是Employee对象。Employee没有 any 属性,因此无需(反)序列化 -
好的,我明白了,谢谢。所以我应该删除包含的 Employee 类?
标签: c# .net json model-view-controller httpclient