Javascript 为WebService将类对象转换为Json
我试图将类Javascript 为WebService将类对象转换为Json,javascript,json,vb.net,web-services,Javascript,Json,Vb.net,Web Services,我试图将类Product的对象作为Json返回给Web服务。该对象加载了所有值,但当它试图将其作为Json返回时,我收到了一个错误。以下是我的方法: <AllowAnonymous> <HttpGet> _ Public Function GetProduct() As HttpResponseMessage Try Dim oProduct As New Product oProduct = Pr
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
有没有办法解决这个问题?使用Newtonsoft.Json类(可通过NuGet获得)进行序列化:
Return Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(oProduct))
产品或继承链中的某些成员可能未初始化。这很容易被忽略,尤其是在VB.NET中 你试过不用StringBuilder吗?e、 g.Dim json as String=jsSerializer.Serialize(opproduct)@AndrewVanTil是的,它也会返回一个错误。如果出现第二个错误,这可能是DotNetNuke的问题,而不是序列化程序本身的问题。您是否尝试过其他JSON框架?我通常使用JSON.NET()。@AndrewVanTil框架没有问题。如果我只指定一个值,比如jsSerializer.Serialize(opproduct.ProductID,sbProduct)@AndrewVanTil,那么我会得到正确的结果,所以问题是获取所有值,而不仅仅是一个
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
Return Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(oProduct))