引号前返回的JSON响应为什么?为什么?

时间:2011-12-07 12:38:26

标签: vb.net json web-services escaping

我有一个返回JSON的REST服务。但是JSON响应在引号(转义字符)之前返回\ _我需要删除那些escapre斜杠!

这是一个示例响应,显示正在发生的事情:

“[{\”Id \“:\”0 \“,\”FirstName \“:\”0firstname \“,\”LastName \“:\”0 lastname \“},{\”Id \“ :\“1 \”,\“FirstName \”:\“1firstname \”,\“LastName \”:\“1 lastname \”},{\“Id \”:\“2 \”,\“FirstName \ “:\”2firstname \“,\”LastName \“:\”2 lastname \“},{\”Id \“:\”3 \“,\”FirstName \“:\”3firstname \“,\”LastName \“:\”3 lastname \“},{\”Id \“:\”4 \“,\”FirstName \“:\”4firstname \“,\”LastName \“:\”4 lastname \“}, {\“Id \”:\“5 \”,\“FirstName \”:\“5firstname \”,\“LastName \”:\“5 lastname \”},{\“Id \”:\“6 \ “,”FirstName \“:\”6firstname \“,\”LastName \“:\”6 lastname \“},{\”Id \“:\”7 \“,\”FirstName \“:\”7firstname \“,\”LastName \“:\”7 lastname \“},{\”Id \“:\”8 \“,\”FirstName \“:\”8firstname \“,\”LastName \“:\” 8 lastname \“},{\”Id \“:\”9 \“,\”FirstName \“:\”9firstname \“,\”LastName \“:\”9 lastname \“},{\”Id \ “:\”10 \“,\”FirstName \“:\”10firstname \“,\”LastName \“:\”10 lastname \“}]”

JAR由Layar解析(http://layar.pbworks.com/w/page/28473583/GetPOIs-Request%20and%20Response%20Examples),所以我不能影响解析,他们不允许这些转义字符

这是我的代码:

** RestServiceImpl.vb

Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Imports System.ServiceModel.Activation
Imports System.Web.Script.Serialization
Imports System.Collections.Generic

Namespace RestService

Public Class Employee
    Public Property Id() As String
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = Value
        End Set
    End Property
    Private m_Id As String
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(value As String)
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(value As String)
            m_LastName = Value
        End Set
    End Property
    Private m_LastName As String
End Class

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class RestServiceImpl
    Implements IRestServiceImpl

    Public Function XMLData(ByVal id As String) As String _
        Implements IRestServiceImpl.XMLData

        Return "XML You requested product " & id

    End Function

    Public Function JSONData(ByVal lat As String, ByVal lng As String, ByVal d As String, ByVal cat As String) As String _
        Implements IRestServiceImpl.JSONData

        Dim json As New JavaScriptSerializer()
        Dim l As New List(Of Employee)

        Dim e As Employee
        For i As Integer = 0 To 10
            e = New Employee
            e.Id = i.ToString
            e.FirstName = i.ToString + "firstname"
            e.LastName = i.ToString + " lastname"
            l.Add(e)
        Next i

        Dim jsonMessage As String = json.Serialize(l.ToArray())
        Return jsonMessage

    End Function

End Class



End Namespace

** IRestServiceImpl.vb

Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Imports System.ServiceModel.Activation

Namespace RestService
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IRestServiceImpl" in both code and config file together.
<ServiceContract()> _
Public Interface IRestServiceImpl
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Xml, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="xml/{id}")> _
    Function XMLData(ByVal id As String) As String


    'WebMessageBodyStyle.Bare WAS WebMessageBodyStyle.wrapped
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare, UriTemplate:="api/objects/json/?lat={lat}&lon={lon}&radius={radius}&cat={cat}")> _
    Function JSONData(ByVal lat As String, ByVal lon As String, ByVal radius As String, ByVal cat As String) As String
    'radius in meters
End Interface


Public Class RawService
    <OperationContract(), WebGet()> _
    Public Function GetValue() As System.IO.Stream
        Dim result As String = "Hello world"
        Dim resultBytes As Byte() = Encoding.UTF8.GetBytes(result)
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"
        Return New MemoryStream(resultBytes)
    End Function

End Class

End Namespace

2 个答案:

答案 0 :(得分:1)

原来如此手动序列化json是不好的:json.Serialize(l.ToArray())。我在这里读到这个:http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/ 所以,我所做的只是返回员工名单,现在一切都很好!

答案 1 :(得分:0)

这些斜杠是引号的转义字符,它正确地执行,因为返回类型是String。您可以更改您的JSONData以返回As List(Of Employee)...

Public Function JSONData(ByVal lat As String, ByVal lng As String, ByVal d As String, ByVal cat As String) As List(Of Employee) _
            Implements IRestServiceImpl.JSONData

            Dim json As New JavaScriptSerializer()
            Dim l As New List(Of Employee)

            Dim e As Employee
            For i As Integer = 0 To 10
                e = New Employee
                e.Id = i.ToString
                e.FirstName = i.ToString + "firstname"
                e.LastName = i.ToString + " lastname"
                l.Add(e)
            Next i


            Return json.Serialize(l.ToArray())

End Function