记录用户行为(json反序列化)

第一步:
我们所需要的框架为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;
}
}

那个保存的地方需要改一下 好了 这样你就可以在文件夹中用记事本打开了,然后用上传就行了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容