【问题标题】:Create hash map from retrofit response从改造响应创建哈希图
【发布时间】:2017-05-05 08:51:06
【问题描述】:

这是我的 JSON 响应

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

我想根据我从服务器获得的响应创建一个哈希映射(id,用户名)并将其存储,以便我可以在所有活动中使用这个哈希映射。

【问题讨论】:

    标签: android json hashmap response retrofit


    【解决方案1】:

    使用这个:-

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

    【讨论】:

      【解决方案2】:

      解析 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));
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-12
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        • 2011-11-26
        • 1970-01-01
        • 2021-06-24
        相关资源
        最近更新 更多