【发布时间】:2018-08-01 10:37:26
【问题描述】:
我正在尝试从 angular 发布的对象反序列化 Web Api 中的对象。我收到一个错误:读取对象引用“1”时出错。路径'Developers[0].DeveloperId',第 20 行,位置 21 我的 Json 对象是(已被验证为有效 JSON):
{
"Id": 0,
"Name": "Name",
"OwnerId": 1,
"Description": "Description",
"Note": "My Notes",
"Stakeholders": [
{
"$id": "1",
"StakeholderId": 1,
"Name": "Mary",
"DateModified": "2018-02-21T12:28:15.023",
"DateCreated": "2018-02-21T12:28:15.023",
"$$hashKey": "object:3"
}
],
"Developers": [
{
"$id": "1",
"DeveloperId": 1,
"DeveloperName": "Joseph",
"DateModified": "2018-02-21T12:28:26.07",
"DateCreated": "2018-02-21T12:28:26.07",
"$$hashKey": "object:4"
}
]
}
我正在尝试反序列化:
var app = JsonConvert.DeserializeObject<Application>(request.ToString(), new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
开发者类(类似于Stakeholder类)
public class Developer : IModificationHistory
{
public int DeveloperId { get; set; }
[Required]
public string DeveloperName { get; set; }
[JsonIgnore]
public virtual List<Application> Applications { get; set; }
public DateTime DateModified { get; set; }
public DateTime DateCreated { get; set; }
}
应用程序类很简单:
public class Application
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Note { get; set; }
public virtual List<Stakeholder> Stakeholders { get; set; }
public int OwnerId { get; set; }
public virtual List<Developer> Developers { get; set; }
}
我用来调用这篇文章的javascript是:
var data =
{
Id: vm.appId,
Name: vm.applicationName,
OwnerId: vm.owner.DeveloperId,
Description: vm.applicationDescription,
Note: vm.note,
Stakeholders: vm.selectedStakeholders,
Developers: vm.selectedDevelopers
};
$http.post("/api/Application/Post/", JSON.stringify(data))
利益相关者列表已正确填写,但开发人员列表未填写。如果我将开发人员放在利益相关者之前的列表中,那么开发人员列表会被正确填写,而利益相关者则不会。任何建议将不胜感激!
【问题讨论】:
-
请贴出
Developer类的代码(我假设它类似于Stakeholders)。 -
好的,我已经添加了课程
-
JSON 格式是否正确?尝试将其粘贴到:jsonformatter.curiousconcept.com
-
好的,我认为这是因为
Developer和Stakeholders都没有$id属性。所以,我猜你必须在发送它们之前去掉这些值,或者你必须告诉JsonConvert忽略这些值(不知道是否可能)。 -
我将 JSON 粘贴到验证器中,它说以两个 {{ 开头无效
标签: c# angularjs json asp.net-web-api