【问题标题】:How to stored the object of array to Shared Preference in Android如何在Android中将数组对象存储到共享首选项
【发布时间】:2016-06-17 13:00:06
【问题描述】:

我必须将 ID 和最小数量转换为存储到共享首选项的数组的 JSON 对象。 我可以从 Volley 中的 JSON 对象中获取字符串 ID 和 Min Quantity。这两项都必须存储在对象的 Shared Preference Array 中,正如我在 JSON 下面提到的那样。

Json:(我想这样存储到共享首选项)

{
  [
    "producutid" : 1
    "minqty"     : 5  
  ] 
  ,
  [
    "producutid" : 2
    "minqty"     : 5  
  ]
,  
 [
    "producutid" : 3
    "minqty"     : 5  
  ]
,  
 [
    "producutid" : 4
    "minqty"     : 5  
  ]       

}

我的工作代码:

   String channel = shared.getString(Constants.cartid, "{'[]','[]'}");
 JSONArray items = null;
 String wishitem;
 JSONObject jo = null;
 if (TextUtils.isEmpty(channel)) {
     jo = new JSONObject();

     try {
         jo.getJSONArray(String.valueOf(0)).put(String.valueOf(productpathid));
         jo.getJSONArray(String.valueOf(1)).put(String.valueOf(minqty));
     } catch (JSONException e) {
         e.printStackTrace();
     }
     wishitem = String.valueOf(items);
     editor.putString(Constants.cartid, wishitem);
     editor.apply();

 } else {

     try {

         Boolean found = false;
         jo = new JSONObject(String.valueOf(channel));
         items = jo.getJSONArray(String.valueOf(0));
         for (int x = 0; x < items.length(); x++) {
             if (productpathid.equalsIgnoreCase(items.getString(x))) {
                 found = true;
             }
         }
         if (!found) {
             try {
                 jo.getJSONArray(String.valueOf(0)).put(String.valueOf(productpathid));
                 jo.getJSONArray(String.valueOf(1)).put(String.valueOf(minqty));
             } catch (JSONException e) {
                 e.printStackTrace();
             }
             wishitem = String.valueOf(items);
             editor.putString(Constants.cartid, wishitem);
             editor.apply();
         }
         editor.apply();

     } catch (JSONException e) {
         e.printStackTrace();
     }}

我有错误

 org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

我犯了一些错误,谁能告诉我这段代码的解决方案。 提前致谢。

【问题讨论】:

    标签: android json android-layout sharedpreferences


    【解决方案1】:

    使用 Google Gson 库来简化这个

    Gson 依赖

    compile 'com.google.code.gson:gson:2.6.1'
    

    在Preference中存储对象的ArrayList

    Gson gson = new Gson();
    ...
    ArrayList<CategoriesEntity> alSelectedCategories = new ArrayList<CategoriesEntity>();
    ...
    String selectedCategory = gson.toJson(alSelectedCategories);
    ApplicationPreferences.setValue("SelectedCategory", selectedCategory, activity);
    

    将其作为 ArrayList 取回

    UserSelectedCategory = ApplicationPreferences.getValue("SelectedCategory", activity);
    if (UserSelectedCategory != null) {
           Type type = new TypeToken<ArrayList<CategoriesEntity>>() {}.getType();
                alSelectedCategories = gson.fromJson(UserSelectedCategory, type);
            }
    

    【讨论】:

    • 是一个模型,我不使用模型来存储共享偏好
    • 你可以采用任何你想要的数据类型
    猜你喜欢
    • 2016-01-17
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2017-01-22
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多