Gail Gail - 2 months ago 14
JSON Question

Decode escaped HTML markup from JSON response on Android in Java

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.

Answer

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>
Comments