Skip to content Skip to sidebar Skip to footer

Android Json Parsing With Html Tags

I want to parse a JSON from a url that is inside tags in android using JsonReader. I have tried multiple examples on Stackoverflow and Androud reference, but i keep g

Solution 1:

I used this method to strip the HTML from the JSON responsehtml: How to strip or escape html tags in Android

publicStringstripHtml(String html)
{
    returnHtml.fromHtml(html).toString();
}

Then retrieved a JSONArray instead of an Object

HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity); 
String noHTML = stripHtml(data);

JSONArray jsonArray = newJSONArray(noHTML);

for(int i = 0; i < jsonArray.length(); i++)
 {
     StopInfo stops = newStopInfo();
     JSONObject jsonObject = jsonArray.getJSONObject(i);

     stops.id = jsonObject.getString("ID");
     stops.name = jsonObject.getString("Name");
     stops.lat = jsonObject.getLong("Lat");
     stops.lng = jsonObject.getLong("Long");
     stops.sms = jsonObject.getString("Sms");

     stopArrayList.add(stops);
 }

Solution 2:

You would need to extract the JSON component from the HTML page. One way of doing it for this structure would be something like:

// If `html` is the full HTML stringStringjson= html.substring(html.indexOf("["), html.lastIndexOf("]") + 1);

Solution 3:

This is web site fault. Json request isn't in html tags. But you can replace it easily from request string. Or you can parse with Jsoup like that:

Documentdoc= Jsoup.Parse(request);
Stringparsedrequest= doc.text();

Solution 4:

You can execute this as http request to server, Do execute it separate thread(may be a asynctask).

HttpClientclient=newDefaultHttpClient();
    HttpGetgetRquest=newHttpGet(urlLink);
    try {
        HttpResponserespnse= client.execute(getRquest);
        StatusLinestatusLine= respnse.getStatusLine();
        StringresponseS= statusLine.getReasonPhrase();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            HttpEntityentity= respnse.getEntity();
            ByteArrayOutputStreamout=newByteArrayOutputStream();
            entity.writeTo(out);

            responseS = out.toString();
            out.close();
            out = null;
        }

Response will be your json string. Your response string is JSONArray so

JSONArray js = newJSONArray(jsonString);

Hope it works

Have a look into your Json data using online json parser

Solution 5:

Looks like youre trying to parse the source code for some reason.

Take a look at this link, it'll guide you through properly parsing JSON formatted response.

Post a Comment for "Android Json Parsing With Html Tags"