wcf rest appending escape characters to HTML tags
I am trying to convert an existing .Net shop site to an Android app.
(This is in VB)
One of the main objects holds product data.
Within that object there is, for instance:
<H2>Product Title</H2>
<P>A description</P>
I have already built a WCF Rest service which returns the data I expect:...
Having said开发者_如何学C that, I have trialled Newtonsoft.Json and DataContractJsonSerializer which produce the same, but different, outputs.
When running the WCF service in debug using Newtonsoft.Json it returns those items as I would expect:
Newtonsoft:
<H2>Product Title<\/H2><P>A description<\/P>
DataContractJsonSerializer:
<H2>Product Title<\\\/H2><P>A description<\\\/P>
However, when I run the Android app through Eclipse I get the error of "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )"
So, in short; how to stop Newtonsoft or DataContractJsonSerializer from inserting these escape sequences?
Thanks Dave
UPDATE:
I have tracked this down to something (?) that WCF is doing. Here is the final bit of my code which returns the JSON string:
retVal = CacheManager.JSONFullProduct("P" & ProductID)
At this point 'retVal' is storing closing HTML tags with just '/'
retVal = retVal.Replace("\/", "/")
At this point 'retVal' is still storing closing HTML tags with just '/'
Return retVal
At this point 'retVal' itself is still storing closing HTML tags with just '/', but when it actually returns (either to Notepad if I'm running the Service direct, or to Android) '/' suddenly becomes '{backslash}/'
I've tried to do a string replace in the android app:
result.replace("\/", "/");
But that returns the same error of "Invalid escape sequence...", and anyway, I don't really want to be doing this kind of work on the phone.
So, what is happening at Return retVal to suddenly insert all of these escape characters??
I have searched high and wide for an answer to this, and have finally found it.
I will share for anyone else experiencing the same issue:
It is the WCF Rest service.
Learning WCF and Android at the same time led me to believe that the response from WCF should be a String serialized in the Json format.
To do this, a .Net object, array or whatever would go through DataContractJsonSerializer before being returned as a String to Android for further parsing.
Something like this:
Dim stream1 As MemoryStream = New MemoryStream
Dim ser As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(myType))
ser.WriteObject(stream1, myThing)
Dim _json As String = Encoding.UTF8.GetString(stream1.ToArray())
stream1.Close()
return _json
Wrong.
Keep your object, array or whatever and return that instead; WCF will take care of the proper escaping for you.
For example (this is VB);
IService:
<OperationContract()> _
<WebGet(BodyStyle:=WebMessageBodyStyle.WrappedRequest, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/MyKit/{AccountID}")> _
Function GetKit(ByVal AccountID As String) As MyKit
Service:
Public Function GetKit(ByVal AccountID As String) As MyKit Implements IService1.GetKit
Dim allKit As New MyKit() //Your object
objDal.CommandText = 'run some sql here - or whatever
Using dr As SqlDataReader = "blah"
//populate your object
End Using
Return allKit //return the object, not the string representation of it
End Function
Using DataContractJsonSerializer for sending as Json to Android from WCF effectively 'pre-escapes' the data. When it gets to Android, the Json parser is unable to handle it, because it also escapes the data.
精彩评论