I have a REST-service that returns JSON. But the JSON response returned with \ before quotes (escape character)....I need those escapre slashes removed!
This is an example response to show what is happening:
"[{\"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\"}]"
The JSON is parsed by Layar (http://layar.pbworks.com/w/page/28473583/GetPOIs-Request%20and%20Response%20Examples), so I cant influence the parsing and they dont allow these escape characters.
Here's my code:
**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
Turns out it's BAD to manually serialize json like this: json.Serialize(l.ToArray()). I was reading this here: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/ So, what I did is just return the list of employees and now its all good!