问题
I need to save my Json Array into SharedPreferences and access it later in different Activity.
here is the example of my Json:
Array
(
[status] => success
[reason] => success
[version] => 1
[total] => Array
(
[category_1] => 3
[category_2] => 3
[category_3] => 3
)
[title] => Array
(
[category_1] => Food
[category_2] => Drink
[category_3] => Dessert
)
[data] => Array
(
[category_1] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
(...
)
[category_2] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
(...
)
[category_3] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
( ...
)
)
)
MainActivity:
...
try {
JSONObject jObject = new JSONObject(result);
String status = jObject.getString("status");
if (status.equals("success")) {
// Log.e("status",status);
JSONObject jObject_data = new JSONObject(jObject.getString("data"));
JSONArray jArray_data_category_1 = new JSONArray(jObject_data.getString("category_1"));
JSONArray jArray_data_category_2 = new JSONArray(jObject_data.getString("category_2"));
//JSONArray jArray_data_category_3 = new JSONArray(jObject_data.getString("category_3"));
//Log.e("category_1 *****************************************", jArray_data_category_1.toString());
vc.insertCategory_1Pref(jArray_data_category_1.toString());
This is my SharedPrefInput.java:
...
public class SharedPrefInput {
public static final String CATEGORY_1 = "CATEGORY_1";
public static final String CATEGORY_2 = "CATEGORY_2";
public static final String CATEGORY_3 = "CATEGORY_3";
...
Here is VarController
...
//category_1
public void insertCategory_1Pref(String s){
prefSetting = context.getSharedPreferences(SharedPrefInput.DEVICE_SETTING, Context.MODE_PRIVATE);
prefSettingEditor = prefSetting.edit();
prefSettingEditor.putString(SharedPrefInput.CATEGORY_1, s);
prefSettingEditor.commit();
}
public String readCategory_1Pref(){
prefSetting = context.getSharedPreferences(SharedPrefInput.DEVICE_SETTING, Context.MODE_PRIVATE);
return prefSetting.getString(SharedPrefInput.CATEGORY_1, "00");
}
...
And here is me trying to access it. but it don't work.
try {
JSONArray jsonArray2 = new JSONArray(sharedCategory_1Pref.getString(CATEGORY_1, status));
for (int i = 0; i < jsonArray2.length(); i++) {
Log.d("your JSON Array", jsonArray2.getInt(i)+"");
Log.e("inside try", "inside try");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("inside Exception", "inside Exception");
}
This is the Log when i try to read the SharePref :
08-12 09:22:34.757: W/System.err(871): at org.json.JSON.typeMismatch(JSON.java:96)
08-12 09:22:34.787: W/System.err(871): at org.json.JSONArray.getInt(JSONArray.java:357)
08-12 09:22:34.787: W/System.err(871): at com.example.brazilapps.MainActivity$1.onPostExecute(MainActivity.java:158)
08-12 09:22:34.787: W/System.err(871): at com.example.brazilapps.MainActivity$1.onPostExecute(MainActivity.java:1)
08-12 09:22:34.787: W/System.err(871): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-12 09:22:34.797: W/System.err(871): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-12 09:22:34.797: W/System.err(871): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-12 09:22:34.797: W/System.err(871): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 09:22:34.808: W/System.err(871): at android.os.Looper.loop(Looper.java:123)
08-12 09:22:34.808: W/System.err(871): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-12 09:22:34.818: W/System.err(871): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 09:22:34.827: W/System.err(871): at java.lang.reflect.Method.invoke(Method.java:507)
08-12 09:22:34.827: W/System.err(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-12 09:22:34.827: W/System.err(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-12 09:22:34.867: W/System.err(871): at dalvik.system.NativeStart.main(Native Method)
08-12 09:22:34.867: E/inside Exception(871): inside Exception
回答1:
I would like to suggest you that when you get Result in JSON convert it into string
and store that string in shared preference as a string and use that string in activity during use convert that saved string into JSON object
using this line JSONObject obj = new JSONObject(json);
and convert JSONObject into string using this line
JSONObject foo = yourJSONOject;
String uid = foo.get("CNo").isString().toString();
String type = foo.get("CName").isString().toString();
or you can use toString to JSONObject so it will convert it into string
回答2:
Save the JSON as String using the JSONArray.toString() to the SharedPreferences using the putString() and retrieve it later with the getString() then calling the new JSONArray(String)
回答3:
Why are you calling jsonArray2.getInt(i)
, while it seems that you array consists of JSONObject
s?
Try jsonArray2.getJSONObject(i)
instead.
Also, this part of the code:
JSONArray jArray_data_category_1 = new JSONArray(jObject_data.getString("category_1"));
could be rewritten as
JSONArray jArray_data_category_1 = jObject_data.getJSONArray("category_1");
回答4:
Your JSON looks like and response from API/Webservices.
Why you want to store such response in Shared Preferences ?
I guess, Just to avoid same api request call in different activity ?
I would suggest you to implement caching like thing. Shared Preferences is not made for storing such big strings.
You can also store that json file in your files folder of your app and read that file whenever required. (Thats what caching does.)
回答5:
Siince you are getting type mismatch in the getInt()
line, instead of the line
Log.d("your JSON Array", jsonArray2.getInt(i)+"");
you can get it as string and parse it to int.
Log.d("your JSON Array", Integer.parseInt(jsonArray2.getString(i))+"");
来源:https://stackoverflow.com/questions/25260456/how-to-save-a-json-array-into-sharedpreferences-and-read-again-in-a-different-ac