JohnnyYin / android-json-intellij-plugin

自动为Model生成一些Json序列化反序列化的方法,如toJson, fromJson。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

android-json-intellij-plugin

主要用于使用Android原生的JsonObject来序列或反序列化Java实体对象。

如:实体类Account:

public class Account {
    public String uid;
    public String username;
    public String email;
    public String gender;
    public transient int localId;
}

使用后,自动生成toJson和fromJson方法:

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class Account {
    public String uid;
    public String username;
    public String email;
    public String gender;
    public transient int localId;

    public JSONObject toJson() {
        try {
            JSONObject json = new JSONObject();
            json.put("uid", uid);
            json.put("username", username);
            json.put("email", email);
            json.put("gender", gender);
            return json;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Account fromJson(String jsonStr) {
        if (TextUtils.isEmpty(jsonStr)) {
            return null;
        }
        Account m = new Account();
        try {
            JSONObject json = new JSONObject(jsonStr);
            m.uid = json.optString("uid");
            m.username = json.optString("username");
            m.email = json.optString("email");
            m.gender = json.optString("gender");
        } catch (JSONException e) {
            e.printStackTrace();
            m = null;
        }
        return m;
    }
}

安装

  1. 下载release包;
  2. 安装:Android Studio->Preferences->Plugins->Install plugin from disk...

About

自动为Model生成一些Json序列化反序列化的方法,如toJson, fromJson。


Languages

Language:Java 100.0%