[英]JSONArrays with android
我有一個可以解析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);
}
如果您需要查看更多代碼,請詢問,我們將很樂意提供。 謝謝。
您可以這樣:
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.