0

我很困惑如何分离JsonArray数据。我想将收到的聊天消息分开,使其在每个气泡中一一出现,而不是变成一个大气泡。
我应该怎么做,将数据彼此分开?
谁能帮帮我吗?提前谢谢你。

我的json部分代码:

if(!content.equals("null")){
                        try{
                            JSONArray jArr = new JSONArray(content);

                              String messages="";
                              for(int i=0; i < jArr.length() ; i++){ 
                                  JSONObject jObj = jArr.getJSONObject(i);


                                     String message = jObj.getString("message");


                                     messages += message+"\n";

                              }

                              showMessage(messages, false);
                        }catch(JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    else{
                        Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
                     }

日志猫:

06-22 09:15:20.486: D/ADBUG(519): content: [{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"testing chat"},{"firstname":"teach","message":"percobaan"},{"firstname":"teach","message":"per"},{"firstname":"teach","message":"tesssssssssss"},{"firstname":"teach","message":"ddffs"},

我希望我的数据变成这样:
在此处输入图像描述

4

1 回答 1

1

换一行像这样:

String messages="";
for(int i=0; i < jArr.length() ; i++){ 
     JSONObject jObj = jArr.getJSONObject(i);

     String message = jObj.getString("message");
     showMessage(messages, false);//move it to here but not outside of 'for'

}
于 2013-06-22T02:47:33.067 回答