我有一个可以解析JSONArray的程序,但前提是jsonarray中有jsonobject,例如:
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}]
我正在尝试解析像这样的jsonarray:
"messages":["This is a demo message. Enjoy!","Another demonstration message stored in JSON format.","JSON stands for JavaScript Object Notation (I think)","hello"]
所以我想知道的是我如何转换我现在能够解析这种json数组的代码。这是我当前解析jsonarray的代码:
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray(/*"actors"*/"weather");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setName(object.getString("id"));
actorsList.add(actor);
}
如果您需要查看更多我的代码,我会很乐意提供。感谢。
答案 0 :(得分:0)
你可以这样做:
if (status == 200)
{
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("messages");
for (int i = 0; i < jarray.length(); i++)
{
String message = jarray.getString(i);
}
}