0

我一直在尝试解析这个无键 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);

}

我目前被困在这里。我应该从这里做什么?

4

1 回答 1

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"
}
于 2013-09-04T00:49:20.300 回答