如何将JsonResult对象作为字符串获取,以便我可以修改它?

Sle*_*lee 8 asp.net asp.net-mvc serialization json

我正在使用FlexiGrid jQuery插件,我需要从我的MVC应用程序中获取一个JSON对象,如果FlexiGrid仅使用该对象,我需要添加一些项目,以便它与FlexiGrid一起正常工作.

所以这是我的控制器代码的一部分:

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
Run Code Online (Sandbox Code Playgroud)

不幸的是,在上面的代码中Json(objCustomerList)返回'System.Web.MVV.JsonResult'而不是所需的JSON字符串数据.我也试着Json(objCustomerList).ToString()看看会发生什么,再次发生同样的事情.

有任何想法吗?

Eri*_*ver 15

Json()ASP.NET MVC中的方法只是通过JavaScriptSerializer类使用JsonResult该类.如果您想使用JSON将objCustomerList对象序列化为字符串,则可以自己使用它.

我的建议是采取略有不同的方法.

  • 创建一个模型,表示您尝试创建的JavaScript对象的.NET等效项.可能是具有Page,Total,Rows和CustomerList属性的FlexiGridModel对象.
  • 然后,当您将FlexiGridModel传递给Json()它时,它就可以正常工作,无需使用构建JSON字符串StringBuilder.

如果你只是想让你的代码工作,那么有一个覆盖JavaScriptSerializer.Serialize(),它使对象序列化StringBuilder并将结果附加到.这应该是你正在寻找的.

一些相关链接:


asl*_*kjo 10

你也可以这样做:

JsonResult json = ... ;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string yourJsonResult = serializer.Serialize(json.Data);
Run Code Online (Sandbox Code Playgroud)

很简单:D

编辑:代码高照明


Sle*_*lee 2

我最终对 Codeproject 示例进行了一些修改:

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
Run Code Online (Sandbox Code Playgroud)

现在在我的控制器中我只需调用:

Return Content(GetFlexiGridJSON(pageIndex, TotalCount, objCustomerList))
Run Code Online (Sandbox Code Playgroud)

只要我传递的对象是一个对象列表,它就可以完美地工作。