2

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?

Raghunandan
  • 131,557
  • 25
  • 223
  • 252
Solorchid
  • 215
  • 5
  • 18

3 Answers3

3

Well I am bit late to answer this, but have wonderful solution for getting json only string from html response in Android

suppose you have a string in variable output

output= Html.fromHtml(output).toString();
output=output.substring(output.indexOf("{"),output.lastIndexOf("}") + 1);

now the output contains only json

JSONObject jsonObject = new JSONObject(output);
Umar Ata
  • 3,889
  • 3
  • 22
  • 34
2

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"}}
Raghunandan
  • 131,557
  • 25
  • 223
  • 252
0

You could read the html with an XML Parser and query the value you're looking for using XPath.

The XPath would be

/div/button/@data-invite-details

Here's a post that explains how to accomplish that in Java/Android:

How to read xml using xpath in java

Community
  • 1
  • 1
Vyrx
  • 723
  • 1
  • 10
  • 14