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?

4
  • use jsoup html parser and get the json Commented Aug 15, 2014 at 18:57
  • @Raghunandan but i think jsoup needs url of the page to get the html of the page, the html code in this question are sent to me as a response, I don't have the url of this html, how should I use jsoup to parse this? Commented Aug 15, 2014 at 19:07
  • you can parse the string response Commented Aug 15, 2014 at 19:13
  • what is u the response that you get a string?? Commented Aug 15, 2014 at 19:17

3 Answers 3

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

Comments

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

2 Comments

oh!!!! thanks a lot! I've never used Jsoup so I didn't know how it works. Thanks for the document!!!
@Solorchid no problem just use the above and it should work fine
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

Comments

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.