【问题标题】:JsonConvert.DeserializeObject Error reading object reference '1'JsonConvert.DeserializeObject 读取对象引用“1”时出错
【发布时间】: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
  • 好的,我认为这是因为DeveloperStakeholders 都没有$id 属性。所以,我猜你必须在发送它们之前去掉这些值,或者你必须告诉JsonConvert 忽略这些值(不知道是否可能)。
  • 我将 JSON 粘贴到验证器中,它说以两个 {{ 开头无效

标签: c# angularjs json asp.net-web-api


【解决方案1】:

问题在于$id的值相同,都设置为1,见内部异常:

{"一个不同的值已经有 Id '1'。"}

我刚刚将它的值更改为 2,它工作正常:

{
  "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": "2",
      "DeveloperId": 1,
      "DeveloperName": "Joseph",
      "DateModified": "2018-02-21T12:28:26.07",
      "DateCreated": "2018-02-21T12:28:26.07",
      "$$hashKey": "object:4"
    }
  ]
}

这是我的输出截图:

【讨论】:

  • 好的,谢谢。 Angular 正在添加 $id 和 $hashKey 所以也许我应该问一个新问题,为什么 Angular 会添加这个?
  • 这仍然没有任何作用,因为它们是 2 个完全不同的列表,他为什么会给出这个错误?
  • @Haytam 我也不知道为什么,但它必须做点什么为什么美元符号$ 前缀。如果我删除并反序列化它可以正常工作。
  • @Haytam 进一步阅读,$ 表示元数据,而不是实际的数据字段。
  • Web api 配置搞砸了,json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;出于某种原因在那里。你的帖子帮助我找到了底线。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 2012-06-09
  • 2012-12-17
相关资源
最近更新 更多