带有HTML标签的Android JSON解析

Android JSON parsing with HTML tags

我想使用JsonReader从android中标记内的URL解析JSON。 我已经在Stackoverflow和Androud参考上尝试了多个示例,但是我不断收到org.json.JSONException: Value 错误或null pointer exception

这是我要解析的URL:链接

这是我使用此示例的代码:如何在Android中解析JSON

我在getData类上得到nullpointexception

JSON解析器类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class JSONParser
{
    public String json ="";
    public InputStream is = null;
    public JSONObject jObj = null;

public JSONObject getJSON(String url)
{
    try
    {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is,"iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line +"
");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
       e.printStackTrace();
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    // return JSON String
    return jObj;
}

MapsActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private void setUpMap()
{
    new getData().execute();
}

class getData extends AsyncTask<String, String, String>
{
    @Override
    protected String doInBackground(String... params)
    {
        JSONParser jsonParser = new JSONParser();
        JSONObject json = jsonParser.getJSON(url);

        try
        {
            String id = json.getString("ID");
            String name = json.getString("Name");
            long lat = json.getLong("Lat");
            long lng = json.getLong("Long");
            String sms = json.getString("Sms");
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}


我使用此方法从JSON响应中剥离HTML:如何在Android中剥离或转义html标签

1
2
3
4
public String stripHtml(String html)
{
    return Html.fromHtml(html).toString();
}

然后检索一个JSONArray而不是一个Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
String noHTML = stripHtml(data);

JSONArray jsonArray = new JSONArray(noHTML);

for(int i = 0; i < jsonArray.length(); i++)
 {
     StopInfo stops = new StopInfo();
     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);
 }

这是网站故障。 Json请求不在html标记中。但是您可以轻松地从请求字符串中替换它。或者,您可以像这样使用Jsoup进行解析:

1
2
Document doc = Jsoup.Parse(request);
String parsedrequest = doc.text();


您可以将其作为对服务器的http请求执行,请单独执行该线程(可能是asynctask)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    HttpClient client = new DefaultHttpClient();
    HttpGet getRquest = new HttpGet(urlLink);
    try {
        HttpResponse respnse = client.execute(getRquest);
        StatusLine statusLine = respnse.getStatusLine();
        String responseS = statusLine.getReasonPhrase();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = respnse.getEntity();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            entity.writeTo(out);

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

响应将是您的json字符串。
您的响应字符串是JSONArray
所以

1
JSONArray js = new JSONArray(jsonString);

希望能奏效

使用在线json解析器查看您的Json数据


您需要从HTML页面提取JSON组件。为此结构执行此操作的一种方法是:

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

看起来您出于某种原因试图解析源代码。

看一下此链接,它将指导您正确解析JSON格式的响应。