2

I am receiving the data from the service with the escape sequence characters...I have managed to elemenate them by this code

results=results.replace("\\\"", "\"");
if(results.startsWith("\"")) {
    results=results.substring(1,results.length());
}
if(results.endsWith("\"")) {
    results=results.substring(0,results.length()-1); 
}

It works fine but for some strings it throws exception while creating json object...How do I automatically unescape the escape characters in the result, I have searched for answers but many of them saying to use a third party library...what is the best I can achieve this.

sschrass
  • 7,014
  • 6
  • 43
  • 62
Pragnani
  • 20,075
  • 6
  • 49
  • 74

2 Answers2

5

I think Apache Commons work pretty good. It has StringEscapeUtils class with bunch of different static methods for escaping and unescaping strings, so i think you should check it.

Good luck!

Evos
  • 3,915
  • 2
  • 18
  • 21
  • Hi @Evos Thanks for the suggestion, I have found about StringEscapeUtils in http://stackoverflow.com/questions/3537706/howto-unescape-a-java-string-literal-in-java but I would like to know ,is there any specific implementation provided by android sdk – Pragnani Jan 31 '13 at 11:37
  • 1
    @Pragnani i'm not 100% sure, but i haven't found one for my project, so i suppose this libarary is one of the best options to do that with a third party library. – Evos Jan 31 '13 at 11:41
  • I will wait for some more answers before accepting yours...Thank you @Evos – Pragnani Jan 31 '13 at 11:43
  • Hi @Evos I have added external jar commons-lang3-3.1 to my project and I have called like this String results=StringEscapeUtils.escapeJava(jsondata); but it throws NoClassDefFound Error, what is wrong..? – Pragnani Jan 31 '13 at 12:16
  • @Pragnani i don't know actually:), try to move jar to `libs` folder (if you are using Eclipse), check that you add it to buildpath via ProjectProperties and execute Clean/Build command. – Evos Jan 31 '13 at 12:20
-1

place this part of code below the parsing Array

                      // to remove all <P> </p> and <br /> and replace with ""
                     content = content.replace("<br />", "");
                     content = content.replace("<p>", "");
                     content = content.replace("</p>", "");

here for me content is object, replace according to ur necessary in the place of "content".

DD.
  • 973
  • 2
  • 10
  • 32