优先保存OBJECT并检索它

时间:2018-02-22 13:25:39

标签: android json gson

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行路径为STRING

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;

  }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

要保存对象,请说出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);