【问题标题】:save OBJECT in preference and also retrieved it优先保存对象并检索它
【发布时间】:2018-02-22 13:25:38
【问题描述】:
jsoncofigdata = "{\n" +
            "  \"configrations\": [\n" +
            "    {\n" +
            "      isofflineserviceRunning: true,\n" +
            "      ispushservicerunning: true,\n" +
            "      isfakegpsrunning: true,\n" +
            "      ispowersavingmodeenable: true,\n" +
            "      iskmviewvisible: true,\n" +
            "      driveronlinefromconfig: true,\n" +
            "      driverofflinefromconfig: true,\n" +
            "      iscancelridebuttonvisible: true,\n" +
            "      ismonthlyfeatureenable: true\n" +
            "    }\n" +
            "  ]\n" +
            "}";

 try {
        JSONObject jObj = new JSONObject(jsoncofigdata);
        if (jObj.has("configrations")) {
            JSONArray jsonArrray = jObj.getJSONArray("configrations");
            if (jsonArrray.length() > 0) {
                ModelOfFunctionalityOnOff.clear();
                for (int i = 0; i < jsonArrray.length(); i++) {
                    JSONObject configrations = jsonArrray.getJSONObject(i);
                     isOfflineServiceRunning = configrations.getBoolean("isofflineserviceRunning");
                     isPushServiceRunning = configrations.getBoolean("ispushservicerunning");
                     isFakeGpsRunning = configrations.getBoolean("isfakegpsrunning");
                     isPowerSavingModeEnable = configrations.getBoolean("ispowersavingmodeenable");
                     isKmViewVisible = configrations.getBoolean("iskmviewvisible");
                     DriverOnlineFromConfig = configrations.getBoolean("driveronlinefromconfig");
                     DriverOfflineFromConfig = configrations.getBoolean("driverofflinefromconfig");
                     IsCancelRideButtonVisible = configrations.getBoolean("iscancelridebuttonvisible");
                     IsMonthlyfeatureEnable= configrations.getBoolean("ismonthlyfeatureenable");

                    ModelOfFunctionalityOnOff.add(new ModelOfFunctionalityOnOff(
                            configrations.getBoolean("isofflineserviceRunning"),
                            configrations.getBoolean("ispushservicerunning"),
                            configrations.getBoolean("isfakegpsrunning"),
                            configrations.getBoolean("ispowersavingmodeenable"),
                            configrations.getBoolean("iskmviewvisible"),
                            configrations.getBoolean("driveronlinefromconfig"),
                            configrations.getBoolean("driverofflinefromconfig"),
                            configrations.getBoolean("iscancelridebuttonvisible"), configrations.getBoolean("ismonthlyfeatureenable")));

                    // Log.d("json", "" + textValues);

                }
                SharedPreferences pref = getSharedPreferences(Data.SHARED_PREF_NAME, 0);
                SharedPreferences.Editor prefsEditor = pref.edit();
                Gson gson = new Gson();
                String json = gson.toJson(jsoncofigdata);
                prefsEditor.putString("configrations", json);
                prefsEditor.commit();

            }

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

数据已保存,但当我通过错误检索对象时。

原因:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为 BEGIN_OBJECT 但在第 1 行第 2 列路径 $

SharedPreferences pref = getSharedPreferences(Data.SHARED_PREF_NAME, 0);

    Gson gson = new Gson();
    String json = pref.getString("configrations", "");
    ModelOfFunctionalityOnOff obj = gson.fromJson(json, ModelOfFunctionalityOnOff.class);

可能是这行代码通过错误

ModelOfFunctionalityOnOff obj = gson.fromJson(json, ModelOfFunctionalityOnOff.class);

模型类

public class ModelOfFunctionalityOnOff {
Boolean isOfflineServiceRunning;
Boolean isPushServiceRunning;
Boolean isFakeGpsRunning;
Boolean isPowerSavingModeEnable;
Boolean isKmViewVisible;
Boolean DriverOnlineFromConfig;
Boolean DriverOfflineFromConfig;
Boolean IsCancelRideButtonVisible;

public Boolean getOfflineServiceRunning() {
    return isOfflineServiceRunning;
}

public void setOfflineServiceRunning(Boolean offlineServiceRunning) {
    isOfflineServiceRunning = offlineServiceRunning;
}

public Boolean getPushServiceRunning() {
    return isPushServiceRunning;
}

public void setPushServiceRunning(Boolean pushServiceRunning) {
    isPushServiceRunning = pushServiceRunning;
}

public Boolean getFakeGpsRunning() {
    return isFakeGpsRunning;
}

public void setFakeGpsRunning(Boolean fakeGpsRunning) {
    isFakeGpsRunning = fakeGpsRunning;
}

public Boolean getPowerSavingModeEnable() {
    return isPowerSavingModeEnable;
}

public void setPowerSavingModeEnable(Boolean powerSavingModeEnable) {
    isPowerSavingModeEnable = powerSavingModeEnable;
}

public Boolean getKmViewVisible() {
    return isKmViewVisible;
}

public void setKmViewVisible(Boolean kmViewVisible) {
    isKmViewVisible = kmViewVisible;
}

public Boolean getDriverOnlineFromConfig() {
    return DriverOnlineFromConfig;
}

public void setDriverOnlineFromConfig(Boolean driverOnlineFromConfig) {
    DriverOnlineFromConfig = driverOnlineFromConfig;
}

public Boolean getDriverOfflineFromConfig() {
    return DriverOfflineFromConfig;
}

public void setDriverOfflineFromConfig(Boolean driverOfflineFromConfig) {
    DriverOfflineFromConfig = driverOfflineFromConfig;
}

public Boolean getCancelRideButtonVisible() {
    return IsCancelRideButtonVisible;
}

public void setCancelRideButtonVisible(Boolean cancelRideButtonVisible) {
    IsCancelRideButtonVisible = cancelRideButtonVisible;
}

public Boolean getMonthlyFeatureEnable() {
    return IsMonthlyFeatureEnable;
}

public void setMonthlyFeatureEnable(Boolean monthlyFeatureEnable) {
    IsMonthlyFeatureEnable = monthlyFeatureEnable;
}

public ModelOfFunctionalityOnOff(Boolean isOfflineServiceRunning, Boolean isPushServiceRunning, Boolean isFakeGpsRunning, Boolean isPowerSavingModeEnable, Boolean isKmViewVisible, Boolean driverOnlineFromConfig, Boolean driverOfflineFromConfig, Boolean isCancelRideButtonVisible, Boolean isMonthlyFeatureEnable) {
    this.isOfflineServiceRunning = isOfflineServiceRunning;
    this.isPushServiceRunning = isPushServiceRunning;
    this.isFakeGpsRunning = isFakeGpsRunning;
    this.isPowerSavingModeEnable = isPowerSavingModeEnable;
    this.isKmViewVisible = isKmViewVisible;
    DriverOnlineFromConfig = driverOnlineFromConfig;
    DriverOfflineFromConfig = driverOfflineFromConfig;
    IsCancelRideButtonVisible = isCancelRideButtonVisible;
    IsMonthlyFeatureEnable = isMonthlyFeatureEnable;
}

Boolean IsMonthlyFeatureEnable;

  }

【问题讨论】:

  • prefsEditor.putString("configrations", json); 改为prefsEditor.putString("configrations", jsoncofigdata);
  • Log.d("configrations",""+obj.getKmViewVisible());在这一行中,我检索了 Null
  • 您的代码中没有这样的行。此外,还不清楚它与从共享首选项中保存和提取字符串有什么关系。
  • 我只想获取保存的getKmViewVisible

标签: android json gson


【解决方案1】:

String json = gson.toJson(jsoncofigdata); 这一行中,您正在将 json 转换为 json。您可以将此变量添加到您的首选项中,例如 prefsEditor.putString("configrations", jsoncofigdata);

【讨论】:

  • Log.d("configrations",""+obj.getKmViewVisible());在这一行中,我检索到 Null
  • @Ahmad 你确定它在 json 源中不为空吗?
  • json 值不为空,它是布尔值,我只想获取已保存的 getKmViewVisible
  • 在您的示例代码中,它被称为 iskmviewvisible ,而不是 isKmViewVisible
  • 我改变了但同样的 null 返回
【解决方案2】:

为了保存你的对象,让我们说 LocationBean -

 String locationJson = new Gson().toJson(locationBean, LocationBean.class);
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(_context);
            SharedPreferences.Editor editor = spref.edit();
            editor.putString(your_key, locationJson);
            editor.apply();

再次获取对象 -

       SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(_context);
    String locationJson  = pref.getString(your_key, defaulValue);
 LocationBean locationBean = new Gson().fromJson(locationJson, LocationBean.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多