У меня есть REST-сервис, который возвращает JSON.Но ответ JSON, возвращенный с кавычками \ before (escape-символ) .... Мне нужно удалить эти косые черты!
Это пример ответа, чтобы показать, что происходит:
"[{\ "Id \": \ "0 \", \ "FirstName \": \ "0firstname \", \ "LastName \": \ "0 фамилия \"}, {\ "Id \": \ "1 \", \ "FirstName \": \ "1firstname \", \ "LastName \": \ "1 фамилия \"}, {\ "Id \": \ "2 \", \ "FirstName \": \ "2firstname \", \" LastName \ ": \" 2 фамилия \ "}, {\" Id \ ": \" 3 \ ", \" FirstName \ ": \" 3firstname \ ", \" LastName \ ": \" 3фамилия \ "}, {\" Id \ ": \" 4 \ ", \" FirstName \ ": \" 4firstname \ ", \" LastName \ ": \" 4 фамилия \ "}, {\" Id \ ": \ "5 \", \ "FirstName \": \ "5firstname \", \ "LastName \": \ "5 lastname \"}, {\ "Id \": \ "6 \", \ "FirstName \": \" 6firstname \ ", \" LastName \ ": \" 6 фамилия \ "}, {\" Id \ ": \" 7 \ ", \" FirstName \ ": \" 7firstname \ ", \" LastName\ ": \" 7 фамилия \ "}, {\" Id \ ": \" 8 \ ", \" FirstName \ ": \" 8firstname \ ", \" LastName \ ": \" 8 фамилия \ "},{\ "Id \": \ "9 \", \ "FirstName \": \ "9firstname \", \ "LastName \": \ "9 фамилия \"}, {\ "Id \": \ "10 \", \" FirstName \ ": \" 10firstname \ ", \" LastName \ ": \" 10 фамилия \ "}]"
JSON анализируется Layar (http://layar.pbworks.com/w/page/28473583/GetPOIs-Request%20and%20Response%20Examples),, поэтому я не могу повлиять на синтаксический анализ, и они не позволяют эти escape-символы.
Вот мой код:
** 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