所需的Json输出Java

所需的Json输出Java,java,mysql,json,Java,Mysql,Json,我不熟悉Java annd Json。我想问一下,如何获得以下Json输出 { "AppData": { "status": "****", "message": [ "" ] }, "Data": { "token": "****" } } 我正在使用下面的代码。 用于从数据库检索数据、导入HashMap和从HashMap检索的代码 HashMap<AppDataRequest, AppData> appdatas = new Ha

我不熟悉Java annd Json。我想问一下,如何获得以下Json输出

{
"AppData": {
    "status": "****",
    "message": [
        ""
    ]
},
"Data": {
    "token": "****"
}
}

我正在使用下面的代码。 用于从数据库检索数据、导入HashMap和从HashMap检索的代码

HashMap<AppDataRequest, AppData> appdatas = new HashMap<AppDataRequest, AppData>();

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://****:3306/****";

static final String USER = "****";
static final String PASS = "****";

public AppDataService(){
    Connection conn = null;
    Statement stat = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stat = conn.createStatement();
        String sql = "SELECT * FROM testdata";
        ResultSet resu = stat.executeQuery(sql);
        while(resu.next()){
            int id = resu.getInt("app_id");
            String email = resu.getString("email");
            String password = resu.getString("password");
            String token = resu.getString("token");
            appdatas.put(new AppDataRequest(id, email, password), new AppData(" ", "success", token));
        }
        resu.close();
        stat.close();
        conn.close();
    }
    catch(SQLException se){
        se.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            if(stat!=null){
                stat.close();
            }
        }
        catch (SQLException se2){

        }
        try{
            if(conn!=null){
                conn.close();
            }
        }
        catch(SQLException se3){
            se3.printStackTrace();
        }
    }       
}

public AppData getSAppData(int id, String email, String password){
    return appdatas.get(new AppDataRequest (id, email, password));
}
我在《邮递员》中的输出是

{
  "message": "****",
  "status": "****",
  "token": "****"
}

我希望得到顶部Json输出中所示的输出。我该怎么做呢?

您的输出是返回数据的Json表示

以下方法返回类型为
AppData
的对象,该对象是
appdatas
映射的值

public AppData getSAppData(int id, String email, String password){
    return appdatas.get(new AppDataRequest (id, email, password));
}

如果要更改输出,请返回相应的数据、键(
AppDataRequest
)对象和值(
AppData
)对象。

您的输出是返回数据的Json表示形式

以下方法返回类型为
AppData
的对象,该对象是
appdatas
映射的值

public AppData getSAppData(int id, String email, String password){
    return appdatas.get(new AppDataRequest (id, email, password));
}
如果要更改输出,请返回相应的数据、键(
AppDataRequest
)对象和值(
AppData
)对象