简体   繁体   中英

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": "\ \ \ Another Message\ \ \ \\n\\n\ \ \ \\n\\n\

\\n\\t\ \\n\\t\\t\ \\n\\t\\t\\t\ asdf\ \\n\\t\\t\\t\ asdfa\ \\n\\t\\t\ \\n\\t\\t\ \\n\\t\\t\\t\ asdf\ \\n\\t\\t\\t\ asdfa\ \\n\\t\\t\ \\n\\t\\t\ \\n\\t\\t\\t\ asdfa\ \\n\\t\\t\\t\ sdfasd\ \\n\\t\\t\ \\n\\t\ \\n\\\n\\n\ \ \ \\n"

Any help would be appreciated. Thanks in advance.

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" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM