0

I'm trying to decode a JSON property that my android app is receiving as escaped HTML. I'm using Java 8 on the latest Android Studio IDE (2.2) and can't find either an Android library from Google or existing java code that would help me solve this problem.

I'm not looking to strip away the HTML, I want to unescape the HTML and then display the HTML, intact, in a TextView. There are many ways to display the HTML properly, but so far only one way to extract the HTML from the escaped String via I library I found called Unbescape on GitHub. I was hoping I wouldn't have to include the library since I only have one JSON property to contend with, it just seems like overkill.

The JSON property is received as:

"HTML": "\u003cp\u003e\u003cu\u003e\u003cstrong\u003eAnother Message\u003c/strong\u003e\u003c/u\u003e\u003c/p\u003e\n\n\u003cp\u003e\u0026#160;\u003c/p\u003e\n\n\u003ctable border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%\"\u003e\n\t\u003ctbody\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003easdf\u003c/td\u003e\n\t\t\t\u003ctd\u003easdfa\u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003easdf\u003c/td\u003e\n\t\t\t\u003ctd\u003easdfa\u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003easdfa\u003c/td\u003e\n\t\t\t\u003ctd\u003esdfasd\u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003cp\u003e\u0026#160;\u003c/p\u003e\n"

Any help would be appreciated. Thanks in advance.

Gail
  • 316
  • 2
  • 13
  • Use a JSON parser. See [How to parse JSON in Android](http://stackoverflow.com/q/9605913/5221149). – Andreas Sep 24 '16 at 22:25
  • I should clarify. I receive an HTTP response using the Retrofit HTTP library. Using the Retrofit RestAdapter.Builder() method and adding a Gson builder instance add as via the setConverter builder step as in .setConverter( GsonConverter( GsonBuilder()..... – Gail Sep 25 '16 at 01:58
  • The JSON response is converted to a Java POJO at this point. In other words there isn't any parsing left to do. It's the unescaping of HTML contained in one of the properties. I know how to add a deserializer to the GsonBuilder call chain and I was hoping to find existing code to perform the unescape operation needed on the HTML. – Gail Sep 25 '16 at 02:08
  • If what you've shown is part of the JSON you get, then the JSON parser will unescape the value for you. If what you've shown is the value *after* the JSON parser has already unescaped it for you, then the value was double-escaped. Best solution is to fix the code to not double-escape. – Andreas Sep 25 '16 at 02:19
  • The reason for the -1 on the question? New feature request for Stack Overflow: all vote-downs require an explanation. This vote-down just seems rather troll-ish. – Gail Apr 03 '17 at 19:14

1 Answers1

2

Assuming what you've shown is a JSON property of a JSON object, you can test JSON parsing very simply.

Create a text file with the content, surrounded by {} to make it a valid JSON object.

{ "HTML": "\u003cp\u003e\u003cu\u003e\u003cstrong\u003eAnother Message\u003c/strong\u003e\u003c/u\u003e\u003c/p\u003e\n\n\u003cp\u003e\u0026#160;\u003c/p\u003e\n\n\u003ctable border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%\"\u003e\n\t\u003ctbody\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003easdf\u003c/td\u003e\n\t\t\t\u003ctd\u003easdfa\u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003easdf\u003c/td\u003e\n\t\t\t\u003ctd\u003easdfa\u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003easdfa\u003c/td\u003e\n\t\t\t\u003ctd\u003esdfasd\u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003cp\u003e\u0026#160;\u003c/p\u003e\n" }

Then run this code, reading the text file.

As you can see below, the JSON parser unescapes everything for you.

public class Test {
    @SerializedName("HTML")
    String html;
    public static void main(String[] args) throws Exception {
        Gson gson = new GsonBuilder().create();
        try (Reader reader = new FileReader("test.json")) {
            Test test = gson.fromJson(reader, Test.class);
            System.out.println(test.html);
        }
    }
}

Output

<p><u><strong>Another Message</strong></u></p>

<p>&#160;</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:100%">
    <tbody>
        <tr>
            <td>asdf</td>
            <td>asdfa</td>
        </tr>
        <tr>
            <td>asdf</td>
            <td>asdfa</td>
        </tr>
        <tr>
            <td>asdfa</td>
            <td>sdfasd</td>
        </tr>
    </tbody>
</table>

<p>&#160;</p>
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you Andreas, your solution worked. I was missing the @SerializedName("HTML") annotation. Excellent test harness....thanks again. – Gail Sep 25 '16 at 13:19