【问题标题】:Remove NewtonSoft JsonSerialization information删除 NewtonSoft JsonSerialization 信息
【发布时间】:2014-08-20 07:49:42
【问题描述】:

当我尝试在 ASP.NET WebApi 中使用 Object 数据类型进行序列化时,包含不需要的属性。

public class SomeViewModel
{
    public SomeViewModel(Object item, SomeItemType itemType)
    {
        Item = item;
        ItemType = itemType;
    }

    public Object Item { get; set; }
    public SomeItemType ItemType { get; set; }
}

使用上述模型,JSON 结果如下。我指出了不需要的属性

{
"$id": "20",
"Result": { // here the item comes.
  "$id": "21",
  "Id": 1005227636,
  "UserName": null,
  "Locale": 0,
  "Grade": 0
},
"Id": 10, // unwanted properties.
"Exception": null,
"Status": 5,
"IsCanceled": false,
"IsCompleted": true,
"CreationOptions": 0,
"AsyncState": null,
"IsFaulted": false
}

如果来自 NewtonSoft.Json 或 ASP.NET 管道,我不知道不需要的属性,但似乎序列化信息与结果一起插入。

如何省略不需要的属性?当我序列化强定义的类时它不会产生。

【问题讨论】:

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


【解决方案1】:

哦,我的错。

Select(async o => item.ThisIsNotAsyncMethod());

linq 中的执行代码有async 关键字,它不是异步任务。删除异步而不是它的工作。 (有点疑惑。我认为async 关键字只是await 的语法糖,不会影响编译结果。也许我误解了)

【讨论】:

  • @AndreasNiedermair // 谢谢你用精彩的文章教我:)
  • ;) 没关系...这只是语法糖(就像所有新东西一样,例如var??select ...)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 2017-08-24
相关资源
最近更新 更多