How can I get a JsonResult object as a string so I can modify it?

I am using the FlexiGrid jQuery plug in and I need to get a JSON object back form my MVC App, simple enough if the FlexiGrid took just the object but I need to add a few items to the response string for it to work properly with FlexiGrid.

So here is a portion of my controller code:

If Request.QueryString("json") IsNot Nothing Then
    Dim data As New StringBuilder()
    data.Append("page: " & pageIndex & "," & vbCrLf)
    data.Append("total: " & ViewData.TotalCount & "," & vbCrLf)
    data.Append("rows: ")
    data.Append(Json(objCustomerList))

    Return Content(data.ToString())
End If

Unfortunately in the above code Json(objCustomerList) returns 'System.Web.MVV.JsonResult' instead of the desired JSON string data. I also tried Json(objCustomerList).ToString() just to see what would happen and the same thing again.

Any ideas?

Answers


Json() method in ASP.NET MVC is just using the JavaScriptSerializer class via the JsonResult class. You could use that yourself if you wanted to serialize the objCustomerList object using JSON to a string.

My recommendation would be to take a slightly different approach.

  • Create a Model that represented the .NET equivalent of the JavaScript object you were trying to create. Maybe a FlexiGridModel object with Page, Total, Rows, and CustomerList properties.
  • Then when you pass that FlexiGridModel to Json() it would just work, no need to build a JSON string using StringBuilder.

If you just want your code to work there is an override on JavaScriptSerializer.Serialize() that takes the object to serialize and the StringBuilder to append the results to. Which should be exactly what you are looking for.

Some relevant links:


You could also do this:

JsonResult json = ... ;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string yourJsonResult = serializer.Serialize(json.Data);

Simple as that :D

edit: code high lighting


I ended up modifiying the Codeproject example a bit:

Imports System.Web.Script.Serialization
Imports System.Reflection

Public Class FlexiGrid

    Public Class FlexigridRow
        Public id As String
        Public cell As New List(Of String)()
    End Class

    Public Class FlexigridObject
        Public page As Integer
        Public total As Integer
        Public rows As New List(Of FlexigridRow)()
    End Class

    Public Shared Function GetFlexiGridJSON(ByVal page As Integer, ByVal total As Integer, ByVal o As Object) As String

        Dim js As New JavaScriptSerializer
        Dim flexiGrid As New FlexigridObject
        Dim i As Integer = 0
        flexiGrid.page = page
        flexiGrid.total = total

        For Each c In o
            Dim r As New FlexigridRow()
            r.id = i
            r.cell = GetPropertyList(c)
            flexiGrid.rows.Add(r)
            i += i
        Next

        Return js.Serialize(flexiGrid)
    End Function

    Private Shared Function GetPropertyList(ByVal obj As Object) As List(Of String)

        Dim propertyList As New List(Of String)()

        Dim type As Type = obj.[GetType]()
        Dim properties As PropertyInfo() = type.GetProperties(BindingFlags.Instance Or BindingFlags.[Public])
        For Each [property] As PropertyInfo In properties
            Dim o As Object = [property].GetValue(obj, Nothing)
            propertyList.Add(If(o Is Nothing, "", o.ToString()))
        Next

        Return propertyList

    End Function

End Class

Now in my controller I just call:

Return Content(GetFlexiGridJSON(pageIndex, TotalCount, objCustomerList))

As long as the object I pass is a list of objects it works perfectly.


This article describes how to get Flexigrid working with MVC step-by-step:

ASP.NET MVC Flexigrid sample


Need Your Help

Secure OpenID user authentication

security authentication openid

My goal is a secure login in system such as stackoverflow uses. I am a newbie but as you have probably seen I have spent all day looking up stackoverflow articles on security. As a result of this