【发布时间】: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 管道,我不知道不需要的属性,但似乎序列化信息与结果一起插入。
如何省略不需要的属性?当我序列化强定义的类时它不会产生。
【问题讨论】:
-
@oakio // 不幸的是,我认为情况并非如此。
标签: c# asp.net-web-api json.net