【发布时间】:2018-10-30 05:58:01
【问题描述】:
我已将JSONArray 作为字符串存储在SharedPreference 中。这是我的JSONObject
JSONObject info = new JSONObject();
try {
info.put("name", full_name);
info.put("phone", foodie_contact);
info.put("no_people", no_peoples);
} catch (JSONException e) {
e.printStackTrace();
}
我将此对象存储在 Array 中,并将 SharedPreference 中的数组存储为
JSONArray array=new JSONArray();
array.put(info.toString());
alert.noInternetAlert(getActivity());
edit=addqueuetemp.edit();
edit.putString("queuedata",array.toString());
edit.apply();
现在,我从 SharedPreference 中获取 JSONArray 并将其解析为
try {
JSONArray array=new JSONArray(addqueuetemp.getString("queuedata","[]"));
for(int i=0;i<array.length();i++){
JSONObject obj=array.getJSONObject(i+1);
Log.i("OBJECT",obj.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
从 Preference 中读取后得到的字符串格式为["{\"name\":\"payal\",\"phone\":\"7427427427\",\"no_people\":\"5\"}"]
我收到错误为Value {"name":"payal","phone":"7427427427","no_people":"5"} at 0 of type java.lang.String cannot be converted to JSONObject
如何解决这个问题?真正的问题在哪里?
【问题讨论】:
-
使用
Gson解析 string 到JSON和从JSON解析。问题是直接从 JSON 解析到 字符串 会附加额外的引号。
标签: android json sharedpreferences jsonexception