我对如何分隔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();
}
logcat的:
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"},
我希望我的数据变成这样:
答案 0 :(得分: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'
}