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 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:
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:
Service:
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.