第一步:
我們所需要的框架為FastJson(不知道Gson可不可以反序列化) 我是一個新手知道走彎路的痛苦,所以我會寫的比較詳細
在項目中加入如下依賴:
compile'com.alibaba:fastjson:1.2.18' fastJsonGithub地址
第二步:
fastJson和Gson一樣需要一個Bean類(數據類)
public class Action {
private String time;
private String from;
private String to;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
Bean中需要get和set的方法,其中的里面的屬性(time from這些)對應的是Json中的字段"from":"zheli",
第三部:儲存用戶行為
這里我建議使用數據庫儲存,數據庫里準備好相應行為的字段
第四部怎么生成Json格式的字符串
這里說一下Json數據的類型一種是
[{“action”:"打開首頁"},{“action”:“退出應用”}]
還有是
{"actions":[{"from":"zheli","time":""2016-11-0221:11:29"","to":"nali"},{"from":"zheli2","time":"2016-11-02","to":"nali2"}]};這種。
還有
{"from":"zheli"}這種。
我們只說前面兩種
第一種很簡單:
首先從數據庫拿出數據并將數據用set的方法放入Bean類中痛也要把Bean類放入集合里
List actionsList =newArrayList<>();
SQLiteDatabase database = new MyDatabase(DateIntentService.this).getWritableDatabase();
Cursor cursor = database.rawQuery("select * from user_Action", null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
Actions actions = new Actions();
actions.setAction_name(cursor.getString(cursor.getColumnIndex("action_name")));
actions.setAction_id(cursor.getString(cursor.getColumnIndex("action_id")));
actions.setDevice(cursor.getString(cursor.getColumnIndex("device")));
actions.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
actions.setActioned_at(cursor.getString(cursor.getColumnIndex("actioned_at")));
actions.setFrom_action(cursor.getString(cursor.getColumnIndex("from_action")));
actions.setDevice_id(cursor.getString(cursor.getColumnIndex("device_id")));
actionsList.add(actions);
}
}
cursor.close();
database.close();
然后正題將數據變成Json格式的字符串:
String json = JSON.toJSONString(actionsList, true); toJSONString方法中的參數,第一個是剛才的集合,第二個是boolean值規定JSON字符串的格式
true:{
"actions":[
{
"from":"zheli",
"time":"\"2016-11-02 21:11:29\"",
"to":"nali"
},
{
"from":"zheli2",
"time":"2016-11-02",
"to":"nali2"
}
]
}
這樣豎著的
false:
[{“action”:"打開首頁"},{“action”:“退出應用”}]
這樣橫著的;
第二種:
在第一種的基礎上在actionsList的外面在弄一個Bean類:
public class Group {
private String android_id;
private boolean isLoginUser;
private String device;
private String uuid;
private String time;
private List<Action> actions=new ArrayList<>();
public String getAndroid_id() {
return android_id; }
public void setAndroid_id(String android_id) { this.android_id = android_id; }
public String getTime() { return time; }
public void setTime(String time) { this.time = time; }
public String getUuid() { return uuid; }
public void setUuid(String uuid) { this.uuid = uuid; }
public String getDevice() { return device; }
public void setDevice(String device) { this.device = device; }
public boolean isLoginUser() { return isLoginUser; }
public void setLoginUser(boolean loginUser) { isLoginUser = loginUser; } public ListgetActions() { return actions; }
public void setActions(Listactions) {
this.actions = actions;
}
public void addActions(Action action) {
actions.add(action);
}
}
這里比第一種多了一個屬性就是
private List<Action>actions=new ArrayList<>();
這個的目的在于存放第二種數據中的actons的集合
還要調用Group的addAction的方法將動作放入 List<Action> actions中
然后調用
String json = JSON.toJSONString(group, false);就行了
結果:
{"actions":[{"from":"zheli","time":"\"2016-11-02 22:09:08\"","to":"nali"},{"from":"zheli2","time":"2016-11-02","to":"nali2"},{"from":"zheli","time":"\"2016-11-02 22:09:32\"","to":"nali"},{"from":"zheli2","time":"2016-11-02","to":"nali2"}],"device":"android","loginUser":false,"time":"時間"};
現在我們就把用戶行為轉換為了Json格式的字符串現在要將字符串放到JSSON文件中
這里就要用到IO流了
private String writeTooJson(String json) {
try{
Date date =new Date();
File file =new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES) +"");
if(!file.exists()) {
file.mkdirs();
}
String path = JSON.toJSONStringWithDateFormat(date,"yyyy-MM-dd_HH_mm_ss").substring(1,20) +".json";
Log.d("ww",path);
File f =new File(file,path);
if(!f.exists()) {
f.createNewFile();
}else{
f.delete();
f.createNewFile();
}
FileWriter fileWriter =newFileWriter(f.getAbsoluteFile());
Buffered Writer bufferedWriter =newBufferedWriter(fileWriter);
buffered Writer.write(json);
buffered Writer.close();
Log.d("ww","done");
returnf.getAbsolutePath();
}catch(IOException e) {
e.printStackTrace();
return null;
}
}
那個保存的地方需要改一下 好了 這樣你就可以在文件夾中用記事本打開了,然后用上傳就行了。