将Class对象转换为WebService的Json

时间:2015-06-27 15:54:34

标签: javascript json vb.net web-services

我正在尝试将Product的对象作为Json返回给Web服务。该对象加载了所有值,但在尝试将其作为Json返回时出现错误。这是我的方法:

 <AllowAnonymous> <HttpGet> _
    Public Function GetProduct() As HttpResponseMessage
        Try
            Dim oProduct As New Product
            oProduct = Product.GetProductByID(25)
            Dim jsSerializer As System.Web.Script.Serialization.JavaScriptSerializer
            jsSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim sbProduct As New System.Text.StringBuilder()
            jsSerializer.Serialize(oProduct, sbProduct)
            Dim json As String = sbProduct.ToString()
            Return Request.CreateResponse(HttpStatusCode.OK, json)
        Catch exc As Exception
            Return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc)
        End Try
    End Function

回复:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Exception has been thrown by the target of an invocation.
</ExceptionMessage>
<ExceptionType>System.Reflection.TargetInvocationException</ExceptionType>
<StackTrace>
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Web.SecurityUtils.MethodInfoInvoke(MethodInfo method, Object target, Object[] args) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output) at Christoc.Modules.top3all.ModuleTaskController.GetProduct() in C:\inetpub\wwwroot\top3\DesktopModules\top3all\Models\WebServices.vb:line 30
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Object reference not set to an instance of an object.
</ExceptionMessage>
<ExceptionType>System.NullReferenceException</ExceptionType>
<StackTrace>
at DotNetNuke.Security.Permissions.PermissionProvider.HasModuleAccess(SecurityAccessLevel accessLevel, String permissionKey, ModuleInfo moduleConfiguration) at DotNetNuke.Security.Permissions.ModulePermissionController.HasModuleAccess(SecurityAccessLevel accessLevel, String permissionKey, ModuleInfo moduleConfiguration) at DotNetNuke.UI.Modules.ModuleInstanceContext.LoadActions(HttpRequest request) at DotNetNuke.UI.Modules.ModuleInstanceContext.get_Actions() at DotNetNuke.Entities.Modules.PortalModuleBase.get_Actions()
</StackTrace>
</InnerException>
</Error>

编辑#1 产品定义

公共类产品     继承AddNew

Public Shared selectedProduct As New Product

Private iProductID As Integer
Public Property ProductID() As Integer
    Get
        Return iProductID
    End Get
    Set(ByVal value As Integer)
        iProductID = value
    End Set
End Property

Private iCategoryID As Integer
Public Property CategoryID() As Integer
    Get
        Return iCategoryID
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iCategoryID = 0
        Else
            iCategoryID = value
        End If
    End Set
End Property

Private iSubCategoryID As Integer
Public Property SubCategoryID() As Integer
    Get
        Return iSubCategoryID
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iSubCategoryID = 0
        Else
            iSubCategoryID = value
        End If
    End Set
End Property

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

Private sDescription As String
Public Property Description() As String
    Get
        Return sDescription
    End Get
    Set(ByVal value As String)
        sDescription = value
    End Set
End Property

Private dPrice As Double
Public Property Price() As Double
    Get
        Return dPrice
    End Get
    Set(ByVal value As Double)
        If IsNothing(value) Or IsDBNull(value) Then
            dPrice = 0.0
        Else
            dPrice = value
        End If
    End Set
End Property

Private sCurrencyCulture As String
Public Property CurrencyCulture() As String
    Get
        Return sCurrencyCulture
    End Get
    Set(ByVal value As String)
        sCurrencyCulture = value
    End Set
End Property

Private sKeywords As String
Public Property Keywords() As String
    Get
        Return sKeywords
    End Get
    Set(ByVal value As String)
        sKeywords = value
    End Set
End Property

Private sCountry As String
Public Property Country() As String
    Get
        Return sCountry
    End Get
    Set(ByVal value As String)
        sCountry = value
    End Set
End Property

Private iViews As Integer
Public Property Views() As Integer
    Get
        Return iViews
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iViews = 0
        Else
            iViews = value
        End If
    End Set
End Property

Private iRating As Integer
Public Property Rating() As Integer
    Get
        Return iRating
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iRating = 0.0
        Else
            iRating = value
        End If

    End Set
End Property

Private dDateAdded As DateTime
Public Property DateAdded() As DateTime
    Get
        Return dDateAdded
    End Get
    Set(ByVal value As DateTime)
        dDateAdded = value
    End Set
End Property

Private sAffiliateLink As String
Public Property AffiliateLink() As String
    Get
        Return sAffiliateLink
    End Get
    Set(ByVal value As String)
        sAffiliateLink = value
    End Set
End Property

Private sAffiliateID As String
Public Property AffiliateID() As String
    Get
        Return sAffiliateID
    End Get
    Set(ByVal value As String)
        sAffiliateID = value
    End Set
End Property

Private iAddedBy As Integer
Public Property AddedBy() As Integer
    Get
        Return iAddedBy
    End Get
    Set(ByVal value As Integer)
        iAddedBy = value
    End Set
End Property

Private iImage As Integer
Public Property Image() As Integer
    Get
        Return iImage
    End Get
    Set(ByVal value As Integer)
        iImage = value
    End Set
End Property

Private iThumbnail As Integer
Public Property Thumbnail() As Integer
    Get
        Return iThumbnail
    End Get
    Set(ByVal value As Integer)
        iThumbnail = value
    End Set
End Property

Private iModifiedBy As Integer
Public Property ModifiedBy() As Integer
    Get
        Return iModifiedBy
    End Get
    Set(ByVal value As Integer)
        iModifiedBy = value
    End Set
End Property


Private dtModifiedTimeUtc As DateTime
Public Property ModifiedTimeUtc() As DateTime
    Get
        Return dtModifiedTimeUtc
    End Get
    Set(ByVal value As DateTime)
        dtModifiedTimeUtc = value
    End Set
End Property

Private sSource As String
Public Property Source() As String
    Get
        Return sSource
    End Get
    Set(ByVal value As String)
        sSource = value
    End Set
End Property

Private iModuleID As Integer
Public Property ModID() As Integer
    Get
        Return iModuleID
    End Get
    Set(ByVal value As Integer)
        iModuleID = value
    End Set
End Property

Private iPortalID As Integer
Public Property PortID() As Integer
    Get
        Return iPortalID
    End Get
    Set(ByVal value As Integer)
        iPortalID = value
    End Set
End Property

任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

使用Newtonsoft.Json类(可通过NuGet获得)来序列化:

Return Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(oProduct))

答案 1 :(得分:0)

Product或继承链中的某些成员可能未初始化。这很容易错过,特别是在VB.NET中。