【发布时间】:2014-03-21 02:04:30
【问题描述】:
开发一个新的 MVC4 应用程序,我在 JSON.net 网站上关注了这个example,用一个新的 JSON JObject 填充我的视图模型:
FinalViewModel finalVM= new FinalViewModel();
IList<ResultModel> results = GetResultList();
FinalVM.QueryResults = results;
JObject myJSON = new JObject(
new JProperty("grid",
new JArray(
from g in results
group g by g.ResultYear into y
orderby y.Key
select new JObject {
new JProperty("Year", y.Key),
new JProperty("min_1", y.Min(g=> g.Result_val1)),
new JProperty("min_2", y.Min(g=> g.Result_val2)),
new JProperty("items",
new JArray(
from g in results
where g.ResultYear==y.Key
orderby g.id
select new JObject(
new JProperty("l", g.Result_val1),
new JProperty("j",g.Result_val2),
new JProperty("id", g.id)
)
)
)}
)));
FinalVM.DataJson = myJSON;
return PartialView("_JSONView", FinalVM);
一切正常,我将这种类型的 json 发送到我的视图:
{
"grid": [
{
"Year": 1998,
"min_val1": "12",
"min_val2": null,
"items": [
{
"l": 12,
"j": null,
"id": 60
},
{
"l": 25,
"j": null,
"id": 61
}
]
}
]
}
我想在空值存在时删除它们。我阅读了很多关于 NullValueHandling 选项的内容,但没有看到如何在我的 Json.Net linq 代码中使用它。
【问题讨论】:
-
我的方法是首先从服务层获取实际对象,然后序列化该对象,而不是搞砸
JProperty..
标签: linq asp.net-mvc-4 json.net