[英]Parse JSON using Iterator and set it in listview accordingly
我正在開發一個具有JSON
響應的應用程序,如下所示:
{
"notifications":{
"0":{
"text":"First One",
"state":"new"
},
"1":{
"text":"Second One",
"state":"new"
},
"2":{
"text":"Third One",
"state":"new"
},
"3":{
"text":"Fourth One",
"state":"old"
},
"4":{
"text":"Fifith One",
"state":"old"
}
}
}
我正在使用Iterator
類來解析此響應。 我正在做這樣的事情:
notifyList = new ArrayList<HashMap<String, String>>();
try {
JSONObject rootObj = new JSONObject(result);
JSONObject jSearchData = rootObj.getJSONObject("notifications");
Iterator<String> keys = jSearchData.keys();
while (keys.hasNext()) {
String key = keys.next();
JSONObject jNotification0 = jSearchData.optJSONObject(key);
if (jNotification0 != null) {
String text = jNotification0.getString("text");
String state = jNotification0.getString("state");
HashMap<String, String> map = new HashMap<String, String>();
map.put("text", text);
map.put("state", state);
System.out.println("Text: " + text);
System.out.println("State: " + state);
notifyList.add(map);
}
else {
}
但這給了我混亂的格式的數據,它不是像JSON
響應中那樣明智。
這是打印出來的log
,全都是亂七八糟的:
Text: Fourth One
State: old
Text: Third One
State: new
Text: Second One
State: new
Text: First One
State: new
Text: Fifth One
State: old
我只是使用“狀態”變量並對其進行相應的排序,以便我的“新”狀態首先出現在“舊狀態”之前。 但是,如果我的回復中有2-3個新狀態,這對我沒有幫助。
我嘗試了看起來像這樣的集合代碼:
Collections.sort(notifyList,
new Comparator<Map<String, String>>() {
@Override
public int compare(final Map<String, String> map1,
final Map<String, String> map2) {
int comparison = map1.get("state")
.compareToIgnoreCase(map2.get("state"));
if (comparison == 0)
return 0;
else if (comparison > 0)
return 1;
else
return -1;
}
}
);
任何想法如何解決這個問題,我想明智地顯示響應順序?
任何幫助將不勝感激。
我建議將“ 1”,“ 2”,...值替換為Json Array,以使其看起來像:
{
"notifications":[
{
"text":"First One",
"state":"new"
},
{
"text":"Second One",
"state":"new"
},
{
"text":"Third One",
"state":"new"
},
{
"text":"Fourth One",
"state":"old"
},
{
"text":"Fifith One",
"state":"old"
}
]
}
這樣,您可以遍歷數組而無需擔心元素順序
第一:您的地圖有誤。 您通過以下方式放置鍵/值:
key | value
---------------------------
text | First One
state | new
text | second one
state | new
text | Third one
state | new
text | Fourth one
state | old
因此,所有值都將被覆蓋(將它們使用相同的鍵放置)。
第二:您需要一個String
的比較器,而不是Map<String, String>
。
List<String> sortedKeys = new ArrayList<>();
sortedKeys.addAll(map.keySet()); //sorted keys will hold all keys for the map
Collection.sort(
sortedKeys,
new Comparator<String>(){
int compare(String s1, String s2) {
return comparison = map.get(s1)
.compareToIgnoreCase(map.get(s2));
}
});
//here you'll have sortedKeys - with keys sorted by the state.
// just get the values from the map iterating it the sortedKeys.
您的數據似乎應該采用數組格式。 但是,作為一種解決方法,如果您總是要獲取數字作為鍵,則可以創建一個輔助類Notification
。 那將有3個字段: key
, text
, state
。 而不是返回Map
List
,而是返回List<Notification>
notifyList = new ArrayList<Notification>();
while (...) {
...
String text = jNotification0.getString("text");
String state = jNotification0.getString("state");
Notification notification = new Notification(key, text, state);
notifyList.add(notification);
}
然后,您可以使用Comparator
key
字段進行排序,以獲得與文本中相同的順序,或者按您喜歡的狀態與鍵之間的任意組合進行排序(例如,按狀態對一階進行排序,即對處於相同狀態的項保持相同的順序)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.