首先,创建
jsonobject
用你的
result
一串
try {
JSONObject MyObject=new JSONObject("{\"NEW_DISPLAY_SCHEDULE\":[{\"ScheduleDate\":\"2018-03-01\",\"StartTime\":\"9:10 am\",\"Endtime\":\"11:30 am\"},{\"ScheduleDate\":\"2018-03-02\",\"StartTime\":\"2:00 pm\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-03\",\"StartTime\":\"5:00 pm\",\"Endtime\":\"6:00 pm\"},{\"ScheduleDate\":\"2018-03-04\",\"StartTime\":\"2:00 pm\",\"Endtime\":\"6:00 pm\"},{\"ScheduleDate\":\"2018-03-05\",\"StartTime\":\"RTO\",\"Endtime\":\"\"},{\"ScheduleDate\":\"2018-03-06\",\"StartTime\":\"10:00 am\",\"Endtime\":\"11:00 pm\"},{\"ScheduleDate\":\"2018-03-06\",\"StartTime\":\"2:00 pm\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-31\",\"StartTime\":\"6:00 am\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-14\",\"StartTime\":\"7:00 am\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-28\",\"StartTime\":\"1:00 pm\",\"Endtime\":\"4:00 pm\"}]}");
} catch (JSONException e) {
e.printStackTrace();
}
那就这样做
SharedPreferences preferences = getSharedPreferences("identifier", Context.MODE_PRIVATE);
在第一个活动中,执行此操作以保存
Editor prefsEditor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyResultObject", json);
prefsEditor.commit();
在第二个活动中,像这样检索它
Gson gson = new Gson();
String json = preferences.getString("MyResultObject", "");
JSONObject obj = gson.fromJson(json, JSONObject.class);
现在要从这个json对象获取任何密钥,
try {
JSONArray jsonArray=obj.getJSONArray("NEW_DISPLAY_SCHEDULE");
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String ScheduleDate=jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
//add toast here to display the value of ScheduleDate in first array element
Toast.makeText(Main2Activity.this , "ScheduleDates"+ ScheduleDate, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
注意:首先初始化此项(>)&燃气轮机;
private ArrayList<String> ScheduleDateArray=new ArrayList<>();