항상있을 수있는 유일한 방법은 messagesByDate obj입니다. "15 May 2012"와 같은 이름의 배열 및 개체는 해당 날짜에 대한 메시지가 있거나없는 기반 서버 (제어 없음)에 의해 생성됩니다.Android :이 json 응답을 어떻게 분석해야합니까?
다른 날짜가 번호가 매겨진 다른 개체를 포함하는 개체 인 반면, 표시된 첫 번째 날짜는 배열입니다.
질문 1 : 어떤 날짜를 알지 못하면 어떻게 파싱합니까?
질문 2 : 일부 메시지는 개체 대신 배열에 있습니다. 어떻게 하나의 ArrayList에 모두 함께 넣을 수 있습니까? 오히려 그것의 배열에서 배열이 항상 거기에 없었기 때문에 또는 아닙니다.
나는 내 마지막 머리까지
감사 해요로 어떤 도움을 주시면 감사하겠습니다하시기 바랍니다.
{
"messagesByDate":{
"15 May 2012":[
{
"id":"1383483367",
"conversation_id":"274618561",
"user_id":"4318264",
"message":"ok will do",
"date_sent":"1337133515",
"date_sent_ago":"7 mins ago"
},
{
"id":"1380222533",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"ok well hmu",
"date_sent":"1337085122",
"date_sent_ago":"13 hrs ago"
},
{
"id":"1380172978",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"superhead",
"date_sent":"1337083910",
"date_sent_ago":"13 hrs ago"
},
{
"id":"1380130860",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"you ready B",
"date_sent":"1337082797",
"date_sent_ago":"14 hrs ago"
},
{
"id":"1378841432",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"hit my cell tho",
"date_sent":"1337054524",
"date_sent_ago":"22 hrs ago"
},
{
"id":"1378836763",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"whats up baby",
"date_sent":"1337054475",
"date_sent_ago":"22 hrs ago"
}
],
"12 May 2012":{
"6":{
"id":"1362948558",
"conversation_id":"274618561",
"user_id":"4318264",
"message":"ok ima text u",
"date_sent":"1336819668",
"date_sent_ago":"3 days ago"
}
},
"11 May 2012":{
"7":{
"id":"1361356267",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"yea thats cool",
"date_sent":"1336790738",
"date_sent_ago":"3 days ago"
},
"8":{
"id":"1357783913",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"sorry im here. would u like to exchange numebers instead?",
"date_sent":"1336722533",
"date_sent_ago":"4 days ago"
},
"9":{
"id":"1357759262",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"hello?",
"date_sent":"1336721851",
"date_sent_ago":"4 days ago"
}
}
}
}
대답은 그렇다고-좀
JSONObject dateHolder = r.getJSONObject("messagesByDate");
Iterator holderItr = dateHolder.keys();
while(holderItr.hasNext()){
String thisdate = holderItr.next().toString();
Object date = dateHolder.get(thisdate);
if (date instanceof JSONArray) {
System.out.println(thisdate+" is an ARRAY.");
JSONArray jarray = (JSONArray) date;
for(int x=0;x<jarray.length();x++){
String msgId = jarray.getJSONObject(x).getString("id");
String msgConvoId = jarray.getJSONObject(x).getString("conversation_id");
String msgUserId = jarray.getJSONObject(x).getString("user_id");
String msgBody = jarray.getJSONObject(x).getString("message");
String msgDateSent = jarray.getJSONObject(x).getString("date_sent");
String msgDateSentAgo = jarray.getJSONObject(x).getString("date_sent_ago");
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("msgId",msgId);
temp.put("msgUserId", msgUserId);
temp.put("msgBody", msgBody);
temp.put("msgDateSent", msgDateSent);
temp.put("msgDateSentAgo", msgDateSentAgo);
messages.add(temp);
}
} else {
System.out.println(thisdate+" is an OBJECT.");
JSONObject jobj = (JSONObject) date;
Iterator insideDate = jobj.keys();
while(insideDate.hasNext()){
String number = insideDate.next().toString();
System.out.println(number);
String msgId = jobj.getJSONObject(number).getString("id");
String msgConvoId = jobj.getJSONObject(number).getString("conversation_id");
String msgUserId =jobj.getJSONObject(number).getString("user_id");
String msgBody = jobj.getJSONObject(number).getString("message");
String msgDateSent = jobj.getJSONObject(number).getString("date_sent");
String msgDateSentAgo = jobj.getJSONObject(number).getString("date_sent_ago");
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("msgId",msgId);
temp.put("msgUserId", msgUserId);
temp.put("msgBody", msgBody);
temp.put("msgDateSent", msgDateSent);
temp.put("msgDateSentAgo", msgDateSentAgo);
messages.add(temp);
}
}
}
이 내게는 HashMap에있는 모든 메시지를 제공하고 ArrayList에에 추가 내가 원하는 같은 메시지라고하지만 날짜별로 순서는 부족합니다. json은 날짜순으로 나열됩니다 ... json 읽기를 지시하는 방법이 있다면 누구든지 알 수 있습니까? 또는 WHILE 및 FOR 루프 순서가 잘못 되었습니까? 해시 맵을 키별로 정렬 할 수 있습니까? 나는 구글거야 ...
Eclipse는 for 문이 msgJson.keys()에 대해 변수 또는 iterable 인스턴스를 사용해야 함을 알립니다. – TonyCruze
Java에서 이전 스타일 반복을 사용하도록 변환하는 것은 꽤 쉽습니다. 나는 이번에 코드를 업데이트했다. 내 머리 꼭대기에서이 작업을하고 있다는 것을 명심하십시오. 나는 이것을 테스트하지 않을 것이므로 이렇게 간단한 구문 변경은 독자가 독자적으로 할 수 있어야하는 것입니다. – chubbsondubs
예전에 FOR 문 대신 Iterator next()와 hasNext()를 사용하기 시작했으며 꽤 가까워졌습니다. 하지만 두 가지를 결합하지 않도록 노력하겠습니다. 내가 놓친 중요한 부분은 단순히 '인스턴스'인지 테스트하는 것입니다. 나는이 예제를 살펴보고 테스트 해보고 작동하도록 할 수 있는지 알아 봅니다. 너무 고마워. – TonyCruze