0
我目前正在創建Android聊天應用程序。在這裏,我從Web服務中獲得了以下json。我想要的是創建聊天的部分日期,以便聊天是在特定日期下的組。如何按日期將聊天記錄分組
{
"messages":{
"rows":[
{
"createdDate":"2014-09-13 13:14:07",
"cID":"36",
"message":"again with me this is not yet the true chat on android.",
"mDate":"2014-09-13 11:14:07",
"senderID":"100000876519562",
"readStatus":"2",
"ID":"156",
"recieverID":"100001031525354"
},
{
"createdDate":"2014-09-15 12:57:44",
"cID":"36",
"message":"this is so long long text on the chat application for me",
"mDate":"2014-09-15 10:57:44",
"senderID":"100000876519562",
"readStatus":"2",
"ID":"158",
"recieverID":"100001031525354"
},
{
"createdDate":"2014-09-15 13:32:42",
"cID":"36",
"message":"is it OK, if I try more and more.",
"mDate":"2014-09-15 11:32:42",
"senderID":"100001031525354",
"readStatus":"2",
"ID":"159",
"recieverID":"100000876519562"
},
{
"createdDate":"2014-09-15 18:38:57",
"cID":"36",
"message":"what if I typed so long long text over here again and again.",
"mDate":"2014-09-15 16:38:57",
"senderID":"100000876519562",
"readStatus":"2",
"ID":"166",
"recieverID":"100001031525354"
},
{
"createdDate":"2014-09-16 11:22:13",
"cID":"36",
"message":"hi this is something uncommon",
"mDate":"2014-09-16 09:22:13",
"senderID":"100000876519562",
"readStatus":"2",
"ID":"167",
"recieverID":"100001031525354"
},
{
"createdDate":"2014-09-16 13:21:55",
"cID":"36",
"message":"it's really",
"mDate":"2014-09-16 11:21:55",
"senderID":"100000876519562",
"readStatus":"2",
"ID":"170",
"recieverID":"100001031525354"
},
{
"createdDate":"2014-09-16 17:25:10",
"cID":"36",
"message":"yes sure",
"mDate":"2014-09-16 15:25:10",
"senderID":"100000876519562",
"readStatus":"2",
"ID":"183",
"recieverID":"100001031525354"
}
]
},
"section":{
"rows":[
{
"MessageDate":"2014-09-13",
"Num":"1"
},
{
"MessageDate":"2014-09-15",
"Num":"3"
},
{
"MessageDate":"2014-09-16",
"Num":"3"
}
]
}
}
這是我試過的。輸出爲嵌套循環的第二,第三...循環再次從零索引開始。任何想法請幫助。
try {
JSONObject jObj = new JSONObject(content);
JSONObject obj = jObj.getJSONObject("messages");
JSONArray arr = obj.getJSONArray("rows");
JSONObject objSec = jObj.getJSONObject("section");
JSONArray arrSec = objSec.getJSONArray("rows");
List<MsgChat> msgList = new ArrayList<MsgChat>();
MsgChat ch = new MsgChat();
for (int i = 0; i < arrSec.length(); i++) {
JSONObject objHis = arrSec.getJSONObject(i);
ch.setChatSection(objHis.getString("MessageDate"));
Log.d("Number of Message", objHis.getString("Num"));
int chatNum = arrSec.getJSONObject(i).getInt("Num");
for(int j = 0; j < chatNum; j++) {
JSONObject messages = arr.getJSONObject(j);
ch.setMsgId(messages.getInt("ID"));
ch.setChatMsg(messages.getString("message"));
Log.d("Message", messages.getString("message"));
ch.setChatDate(messages
.getString("createdDate"));
ch.setSenderId(Long.parseLong(messages
.getString("senderID")));
msgList.add(ch);
}
msgList.add(ch);
}
return msgList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
你的代碼似乎沒有很好的準備。我嘗試過,但在這裏Format.parse(a1)說錯誤「方法解析(字符串)是未定義的類型格式」 – Daroath 2014-09-25 01:39:12
我沒有添加代碼之前formet,bcs你我留下空間自己添加代碼,無論如何,我已經編輯了上面的代碼。 – prakash 2014-09-25 04:16:19