到目前为止,我已经看到了WebMatrix的Json Helper的基本用法,如here和here。这些示例显示了如何将List直接转换为简单的JSON,但我想知道如何创建更加自定义的JSON。
我所说的是这些示例将List编码为JSON,如下所示:
[
{obj1_property1 : obj1_value1,...},
{obj2_property1 : obj2_value1,...},
....,
{objN_property1 : objN_value1,...}
]
我想要创建的内容如下:
{
customProperty1 : customPropertyValue1,
customProperty2 : customPropertyValue2,
entries : [
{obj1_property1 : obj1_value1,...},
{obj2_property1 : obj2_value1,...},
....,
{objN_property1 : objN_value1,...}
]
}
如何使用Json Helper创建这种JSON?或者我需要使用其他东西吗?谢谢你的帮助。
答案 0 :(得分:0)
如果你没有找到更好的方法,那么设置一个独立的剃刀页面.cshtml就像下面这样很容易。然后,URL直接返回JSON字符串。
@{
// write whatever C# code is necessary to get object data
...
}
{
"customProperty1":"@cp1",
"customProperty2":"@cp2",
...
}