开发者

Returning Json Result with object name MVC

开发者 https://www.devze.com 2023-01-22 04:36 出处:网络
When a json result is returned by the controller the object name seems to be missing, I normally wouldn\'t mind but the flexbox jquery plugin requires the json result in a particular format.

When a json result is returned by the controller the object name seems to be missing, I normally wouldn't mind but the flexbox jquery plugin requires the json result in a particular format.

Flexcombobox expected format

{"results":[  
     {"id":"1","name":"Ant"},  
     {"id":"2","name":"Bear"},  
     {"id":"3","name":"Cat"},  
     {"id":"4","name":"Dog"},  
     {"id":"5","name":"Elephant"},  
     {"id":"6","name":"Fox"},  
     {"id":"7","name":"Guinea Pig"},  
     {"id":"8","name":"Horse"},  
     {"id":"9","name":"Iguana"},  
     {"id":"10","name":"Jaguar"}  
 ]} 

Class

Public Class Flexbo开发者_开发知识库xResult

    Private _id As String
    Public Property Id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

End Class

Controller Code

Function PartYearsList() As JsonResult
            Dim yearSelectList As List(Of FlexboxResult) = New List(Of FlexboxResult)

            For index As Integer = DateTime.Now.Year To 1955 Step -1
                yearSelectList.Add(New FlexboxResult() With {.Id = index, .Name = index})
            Next

            Return Me.Json(yearSelectList.ToArray(), JsonRequestBehavior.AllowGet)
End Function

Json result returned (shortened)

[{"Id":"2010","Name":"2010"},{"Id":"2009","Name":"2009"},{"Id":"2008","Name":"2008"}]

Desired result (shortened)

{"results": [{"Id":"2010","Name":"2010"},{"Id":"2009","Name":"2009"},{"Id":"2008","Name":"2008"}]}

Flexcombobox documentation http://www.fairwaytech.com/flexbox.aspx


In C#, you can use an anonymous object to tweak the JSON structure on its way out:

// The ToArray() probably isn't necessary. Collections like List<T> are treated
//  as JavaScript arrays when JavaScriptSerializer turns them into JSON.
return Json(new { results = yearSelectList});

Update:

From Dien, this is the VB syntax for the same thing:

Return Json(New With {Key .results = yearSelectList}, JsonRequestBehavior.AllowGet)


To get fine grain control of the output JSON, you could try declaring a data contract like:

[DataContract]
public class MyResultListContract
{
    [DataMember]
    public List<MyResultContract> results { get; set; }
}

[DataContract]
public class MyResultContract
{
    [DataMember]
    public string Id {get; set;}
    [DataMember]
    public string Name {get; set;}
}

and then using the DataContractJsonSerializer:

var myResults = ...
var serialiser = new DataContractJsonSerializer(typeof(MyResultListContract));
var jsonString = serialiser.WriteAsString(myResults);
0

精彩评论

暂无评论...
验证码 换一张
取 消