2010-09-15 54 views
1

我有一个ASP.net MVC应用程序,它从数据库中获取标记坐标(我使用ActiveRecord)并将它们输出为json以用于谷歌地图。但是格式不太正确。有谁知道我可以如何改变输出?更改Json输出ASP.net MVC

目前的输出是:

[ 
    { 
     "Id": 1, 
     "Name": null, 
     "Location": "13.79194402, 100.71588015" 
    }, 
    { 
     "Id": 2, 
     "Name": null, 
     "Location": "13.79194402, 100.71588015", 
... 

它应该是:

{ 
"locations": [ 
    { 
     "Id": 1, 
     "Name": null, 
     "Location": "13.79194402, 100.71588015" 
    }, 
    { 
     "Id": 2, 
     "Name": null, 
     "Location": "13.79194402, 100.71588015", 
... 

代码控制器:

public ActionResult Map() 
    { 
     var map = DeviceLocation.FindAll(); 
     return Json(map, JsonRequestBehavior.AllowGet); 
    } 

我如何与数据库通信:

[ActiveRecord("Location")] 
    public class DeviceLocation : ActiveRecordValidationBase<DeviceLocation> 
    { 
     private int _id; 
     private string _name; 
     private string _location; 

     [PrimaryKey("Id")] 
     public int Id 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     [Property("Name")] 
     public string Name 
     { 
      get { return _name; } 
      set { _name = value; } 
     } 

     [Property("Coords")] 
     public string Location 
     { 
      get { return _location; } 
      set { _location = value; } 
     } 

回答

1

这应该做的工作:

public ActionResult Map() 
{ 
    var map = DeviceLocation.FindAll(); 
    var locations = new { Locations = map }; 
    return Json(locations, JsonRequestBehavior.AllowGet); 
} 

返回的位置之前,将其分配给一个匿名类型的位置特性。这将导致JsonResult按照您希望的方式格式化输出。

+0

谢谢它做到了:) – Prd 2010-09-16 12:24:46