如何将 Gson 的值放入 HashMap 中

huangapple go评论78阅读模式
英文:

How to put Gson value into HashMap

问题

以下是已翻译的部分:

我有一个Firebase数据库,我想在我的应用程序中存储用户信息。在我的Firebase数据库中,我有4个字符串和1个类似于以下内容的数组列表:

(图片链接)

我想使用SharedPreferences将这些值存储在我的应用程序中。我已经检索了手机号码、出生日期、名字和性别,并将它们存储在一个HashMap中。但是我无法获取用户的爱好信息。我使用了Gson库来处理用户的爱好,但我不知道如何与其他字符串值一起使用。

public static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_NAME = "name";
public static final String KEY_BIRTHDATE = "birthdate";
public static final String KEY_GENDER = "gender";
public static final String KEY_PHONE = "phone";
public static final String KEY_HOBBIES = "hobbies";

public SessionManager(Context _context) {
    context = _context;
    usersSession = _context.getSharedPreferences("userLoginSession", Context.MODE_PRIVATE);
    editor = usersSession.edit();
}

public void createLoginSession(String name, String birthdate, String gender, String phone, ArrayList<String> hobby) {
    Gson gson = new Gson();
    String json = gson.toJson(hobby);

    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_NAME, name);
    editor.putString(KEY_BIRTHDATE, birthdate);
    editor.putString(KEY_GENDER, gender);
    editor.putString(KEY_PHONE, phone);
    editor.putString(KEY_HOBBIES, json);
    editor.commit();
}

public HashMap<String, String> getUsersDetailFromSession() {
    HashMap<String, String> userData = new HashMap<String, String>();
    userData.put(KEY_NAME, usersSession.getString(KEY_NAME, null));
    userData.put(KEY_BIRTHDATE, usersSession.getString(KEY_BIRTHDATE, null));
    userData.put(KEY_GENDER, usersSession.getString(KEY_GENDER, null));
    userData.put(KEY_PHONE, usersSession.getString(KEY_PHONE, null));
    return userData;
}

createLoginSession中,我定义了一个gson变量,并像其他字符串一样使用putString将值放入KEY_HOBBIES中。我应该如何在HashMap函数中将用户的兴趣爱好与其他字符串一起放置?我希望能够加载和读取HashMap中的所有数据。感谢您的理解,我的英文不太好。

英文:

I have a Firebase database and I want to store users information in my app. In my firebase database, I have 4 string and 1 arraylist like this..

如何将 Gson 的值放入 HashMap 中

I want to store these values in my app with using SharedPreferences. I did retrieve Phone number, Birthdate, First name and Gender and I stored into a Hashmap. But I couldn't get User hobbies. I use Gson library for User hobbies, but I don't know how to use with other string values.

public static final String IS_LOGIN = &quot;IsLoggedIn&quot;;
public static final String KEY_NAME = &quot;name&quot;;
public static final String KEY_BIRTHDATE = &quot;birthdate&quot;;
public static final String KEY_GENDER = &quot;gender&quot;;
public static final String KEY_PHONE = &quot;phone&quot;;
public static final String KEY_HOBBIES = &quot;hobbies&quot;;
public SessionManager(Context _context) {
context = _context;
usersSession = _context.getSharedPreferences(&quot;userLoginSession&quot;, Context.MODE_PRIVATE);
editor = usersSession.edit();
}
public void createLoginSession(String name, String birthdate, String gender, String phone, ArrayList&lt;String&gt; hobby) {
Gson gson = new Gson();
String json = gson.toJson(hobby);
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_NAME, name);
editor.putString(KEY_BIRTHDATE, birthdate);
editor.putString(KEY_GENDER, gender);
editor.putString(KEY_PHONE, phone);
editor.putString(KEY_HOBBIES, json);
editor.commit();
}
public HashMap&lt;String, String&gt; getUsersDetailFromSession() {
HashMap&lt;String, String&gt; userData = new HashMap&lt;String, String&gt;();
userData.put(KEY_NAME, usersSession.getString(KEY_NAME, null));
userData.put(KEY_BIRTHDATE, usersSession.getString(KEY_BIRTHDATE, null));
userData.put(KEY_GENDER, usersSession.getString(KEY_GENDER, null));
userData.put(KEY_PHONE, usersSession.getString(KEY_PHONE, null));
return userData;
}

In createLoginSession, I defined a gson variable and I used putString value to KEY_HOBBIES like other strings. What should I do to put User Hobbies with other strings in Hashmap function? I want to load and read all datas in Hashmap. My english is not very good, thank you for all.

答案1

得分: 2

你可以像这样使用 jsonArray:

JSONArray jsonArray = object.getJSONArray("response");
for (int i = 0; i <= jsonArray.length(); i++) {
    JSONObject obj = jsonArray.getJSONObject(i);
    // 在这里使用你的对象
}
英文:

You can use jsonArray like this

JSONArray jsonArray = object.getJSONArray(&quot;response&quot;);
for (int i = 0; i &lt;= jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
//here you use your objects
}

huangapple
  • 本文由 发表于 2020年8月31日 15:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63666787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定