Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从改造响应创建哈希映射_Android_Json_Hashmap_Response_Retrofit - Fatal编程技术网

Android 从改造响应创建哈希映射

Android 从改造响应创建哈希映射,android,json,hashmap,response,retrofit,Android,Json,Hashmap,Response,Retrofit,这是我的JSON响应 [ { "id": 1, "username": "aaa", }, { "id": 2, "username": "bbb", }, { "id": 3, "username": "ccc", } ] 我想根据从服务器得到的响应创建一个哈希映射(id、用户名),并将其存储,以便我可以在所有活动中使用此哈希映射。使用此:- HashMap hashMap=new HashMap(); hashMap.put(

这是我的JSON响应

[
  {
    "id": 1,
    "username": "aaa",
  },
{
    "id": 2,
    "username": "bbb",
  },
{
    "id": 3,
    "username": "ccc",
  }
]
我想根据从服务器得到的响应创建一个哈希映射(id、用户名),并将其存储,以便我可以在所有活动中使用此哈希映射。

使用此:-

HashMap hashMap=new HashMap();
hashMap.put("id",id);
hashMap.put("username",username);
使用以下命令:-

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

解析JSONObject并创建HashMap:

 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));
   }
}
HashMap userData=newhashmap();
对于(int i=0;i
解析JSONObject并创建HashMap:

 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));
   }
}
HashMap userData=newhashmap();
对于(int i=0;i