Solorchid Solorchid - 4 months ago 16
Android Question

Java/Android how to get JSON from a html response?

I'm getting an html response from HttpGet,the response is like following:

<div class="noti-contents">
<button class="accept-invitation gui-button" data-invite="pi:103158:18:60:114779" data-invite-details='{"f":"103158","p":18,"api":false,"pid":60,"t":114779,"sub":"p10315857a3f8","u":{"id":"103158","name":"xxxxxx","profile_image":"{1}","status":"1"}}'><span>Accept</span></button>
</div>


and all the above code are stored in a string variable 'response'; but now in my app i only need the JSON part of the html:

{"f":"103158","p":18,"api":false,"pid":60,"t":114779,"sub":"p10315857a3f8","u":{"id":"103158","name":"xxxxxx","profile_image":"{1}","status":"1"}}


so how should I parse this string to get the only the above part?

Answer

Have a look at the docs

http://jsoup.org/apidocs/org/jsoup/Jsoup.html

You can use Jsoup to parse html

Document doc = Jsoup.parse("html string");  
Elements elements = doc.select("button");
Log.i("..........",""+elements.attr("data-invite-details"));

Log

08-15 19:16:42.670: I/..........(1612): {"f":"103158","p":18,"api":false,"pid":60,"t":114779,"sub":"p10315857a3f8","u":{"id":"103158","name":"xxxxxx","profile_image":"{1}","status":"1"}}