我有一个 .NET 字典,其中包含来自实体框架中的 SQL 函数的项目。这是我将项目添加到字典的代码:
public FooViewModel GetFoo()
{
var fooresults = new FooResultsViewModel();
using (var db = new DBEntities())
{
var results = db.FunctionResults().ToList();
foreach (var d in results)
{
foo.a.Add(d.ColumnName, d.ColumnValue);
}
}
return fooresults;
这是我的模型:
public class FooViewModel : GraphViewModel
{
public FooResultsViewModel() { }
public Dictionary<string, decimal> a = new Dictionary<string, decimal>();
}
最后这是我的控制器:
public virtual JsonResult GetData()
{
var fooresults = new FactSurveryResultsQueries().GetAverages();
return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = fooresults };
}
这是返回的 JSON:
{"a":
{"FooResult1":3.579831,
"FooResult2":3.359243,
"FooResult3":3.720588,
"FooResult4":3.636554,
"FooResult5":3.285714,
"FooResult6":3.758403,
"FooResult7":3.262605}}
但是我正在寻找返回的东西,例如:
{"a":
{ColumnName:"FooResult1",
ColumnValue:3.579831,
ColumnName:"FooResult2",
ColumnValue:3.359243,
ColumnName:"FooResult3",
ColumnValue:3.720588,
ColumnName:"FooResult4",
ColumnValue:3.636554,
ColumnName:"FooResult5",
ColumnValue:3.285714,
ColumnName:"FooResult6",
ColumnValue:3.758403,
ColumnName:"FooResult7",
ColumnValue:3.262605}}
有没有办法为第二个结果格式化 JSON 输出?