如何仅通过更改Json方法来记录事件数据?

时间:2018-03-22 06:41:09

标签: android json logging

我有一个Android应用程序。

我通过创建一个从应用程序捕获各种参数/变量值的方法来捕获事件。 我编写的代码片段包含EventClass和EventModelClass { 公共类UserAccessEvent实现ILog {

private Logger mLogger = null;
UserAccessEventModel obj;

public void StoreUserAccessEvent(String Timestamp, String UserId, int FailedAttempts, int SuccessfulAttempts, int Attempts,
                                 int NoOfRevocations, int PassCodeChange) {

    obj = new UserAccessEventModel(Timestamp, UserId,  SuccessfulAttempts, FailedAttempts, Attempts, NoOfRevocations, PassCodeChange);

    toJsonLog();
}

public String toJsonLog() {

    JSONObject event = new JSONObject();

    try {

        event.put("Timestamp", obj.getTimestamp());
        event.put("UserId", obj.userid);
        event.put("SuccessfulAttempts", obj.successfulattempts);
        event.put("FailedAttempts", obj.failedattempts);
        event.put("Attempts", obj.attempts);
        event.put("NoOfRevocations", obj.noofrevocations);
        event.put("PassCodeChange", obj.passcodechange);

        //to store the log data that is stored in json object
        Log.d("UserAccessEvent", event.toString());
        return event.toString();

    } catch (Exception e) {
        Log.d("UserAccessEvent", "Exception");
        throw new RuntimeException(e);

    }
}

}

public class UserAccessEventModel {

private String timestamp;
public String userid;
public int successfulattempts;
public int failedattempts;
public int attempts;
public int noofrevocations;
public int passcodechange;


public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

public void setUserid(String userid) {
    this.userid = userid;
}

public void setSuccessfulattempts(int successfulattempts) { this.successfulattempts = successfulattempts;   }

public void setFailedattempts(int failedattempts) {
    this.failedattempts = failedattempts;
}

public void setAttempts(int attempts) {
    this.attempts = attempts;
}

public void setNoofrevocations(int noofrevocations) {
    this.noofrevocations = noofrevocations;
}

public void setPasscodechange(int passcodechange) {
    this.passcodechange = passcodechange;
}

public String getTimestamp(){
    timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    return timestamp;

}

public UserAccessEventModel() {

}

public UserAccessEventModel(String timestamp, String userid, int successfulattempts, int failedattempts, int attempts, int noofrevocations, int passcodechange) {
    this.timestamp = timestamp;
    this.userid = userid;
    this.successfulattempts = successfulattempts;
    this.failedattempts = failedattempts;
    this.attempts = attempts;
    this.noofrevocations = noofrevocations;
    this.passcodechange = passcodechange;
}

}

我想进行更改,以便我可以使用JSON来记录数据,而无需使用模型类。我希望它更通用.....

现在我需要通过更改JSON方法键/值对来记录方法中的数据。

有什么方法可以做到吗??

请提出任何建议。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Java 8,则可以执行以下操作。

public String toJsonLog(Class YourModelClass) {
                JSONObject event = new JSONObject();
                Class yourClass = YourModelClass.class;
                Field[] fields = yourClass.getFields();

                YourModelClass obj = new YourModelClass();

                for(Field f: fields){
                    f.getName();
                }
                try {
                    for(int i=0; i<fields.length(); i++){
                    String geterStr = obj + ".get" + fields[i] + "()";
                        event.put(fields[i], geterStr);
                    }

                    Log.d("YourModelClass", event.toString());
                    return event.toString();

                } catch (Exception e) {
                    Log.d("YourModelClass", "Exception");
                    throw new RuntimeException(e);

                }
            }

但是在这里你必须使getter方法与你的字段名称相同。对于前如果您的字段名称为userId,那么您必须将getter方法设为getuserId()而不是getUserId()。你必须忽略java命名约定才能使它成为通用的。

注意:我还没试过这个,但我从这个问题中引用了How to get variable name? - java并提出了我自己的逻辑,但我想它应该可行。