如何解析Android上的无密钥json数组?

时间:2013-09-04 00:37:36

标签: android arrays json parsing

我一直试图解析这个无钥匙的json数组。我知道有很多关于如何解析json数组的教程,但它们总是遵循相同的模式。我在这里有这个网站,我试图解析并显示文本。 http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php

    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php");
    // Depends on your web service
    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) { 
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }

    JSONObject jObject = new JSONObject(result);

}

我目前被困在这里。我该怎么办?

1 个答案:

答案 0 :(得分:1)

这应该不是那么困难。见下面的代码。 寻找语法错误并处理异常,我只需输入它。

JSONArray jsonArray = jObject.getJSONArray("notices");        
for(int i = 0; i < jsonArray.length(); i++) {
    String string = jsonArray.getString(i);
    Log.d("TAG", string); // do whatever you want with "string"
}