【问题标题】:How can I get a JsonResult object as a string so I can modify it?如何将 JsonResult 对象作为字符串获取,以便对其进行修改?
【发布时间】:2010-09-23 17:15:41
【问题描述】:

我正在使用 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

不幸的是,在上面的代码 Json(objCustomerList) 返回 'System.Web.MVV.JsonResult' 而不是所需的 JSON 字符串数据。我还尝试了Json(objCustomerList).ToString(),只是想看看会发生什么以及同样的事情。

有什么想法吗?

【问题讨论】:

    标签: asp.net asp.net-mvc json serialization


    【解决方案1】:

    你也可以这样做:

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

    就这么简单:D

    编辑:代码高亮

    【讨论】:

      【解决方案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
      

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

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

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

      【讨论】:

        【解决方案3】:

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

        我的建议是采取稍微不同的方法。

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

        如果您只想让您的代码正常工作,则可以使用 override on JavaScriptSerializer.Serialize() 将对象序列化,并使用 StringBuilder 将结果附加到。这应该正是您正在寻找的。​​p>

        一些相关链接:

        【讨论】:

        • 正是我需要的。谢谢勺子和stackoverflow
        猜你喜欢
        • 1970-01-01
        • 2018-02-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-20
        • 2015-06-02
        • 2010-12-19
        • 1970-01-01
        相关资源
        最近更新 更多