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.

5
  • Use a JSON parser. See How to parse JSON in Android. Commented Sep 24, 2016 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()..... Commented Sep 25, 2016 at 1: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. Commented Sep 25, 2016 at 2: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. Commented Sep 25, 2016 at 2: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. Commented Apr 3, 2017 at 19:14

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Andreas, your solution worked. I was missing the @SerializedName("HTML") annotation. Excellent test harness....thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.