2016-12-20 3 views
0

Dies ist meine JSON-Antwortvon Hash-Map erstellen Retrofit Antwort

[ 
    { 
    "id": 1, 
    "username": "aaa", 
    }, 
{ 
    "id": 2, 
    "username": "bbb", 
    }, 
{ 
    "id": 3, 
    "username": "ccc", 
    } 
] 

Ich mag eine Hash-Karte (id, username) von der Antwort, die ich von meinem Server erhalten erstellen und speichern, so dass ich verwenden können, Diese Hash-Karte über alle Aktivitäten.

Antwort

0

Verwendung dieses: -

HashMap hashMap=new HashMap(); 
hashMap.put("id",id); 
hashMap.put("username",username); 
0

die JSONObject Parse und HashMap erstellen:

HashMap<String, String> userData = new HashMap<String, String>(); 
for(int i = 0; i < myArray.length(); i++) { 
    JSONObject j = myArray.optJSONObject(i); 
    Iterator it = j.keys(); 
    while (it.hasNext()) { 
     String n = it.next(); 
     userData.put(n, j.getString(n)); 
    } 
}