Java 结果集转换为 JSON。如何打印 json 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9686732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:08:45  来源:igfitidea点击:

resultset into JSON. How can I print the json Object?

javajsonobjectresultset

提问by Becks

I am referring to this linkbut I have slightly modified my approach..

我指的是这个链接,但我稍微修改了我的方法..

This is the code that I have :

这是我拥有的代码:

 public JSONArray generateJSON(ResultSet rs) {

        JSONArray respJSON = new JSONArray();

        try {
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int numColumns = rsmd.getColumnCount();
            while (rs.next()) {
                JSONObject obj = new JSONObject();
                for (int i = 1; i < numColumns + 1; i++) {

                    String columnName = rsmd.getColumnName(i);
                    if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) {
                        obj.put(columnName, rs.getArray(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
                        obj.put(columnName, rs.getBoolean(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
                        obj.put(columnName, rs.getBlob(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
                        obj.put(columnName, rs.getDouble(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
                        obj.put(columnName, rs.getFloat(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.NVARCHAR) {
                        obj.put(columnName, rs.getNString(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
                        obj.put(columnName, rs.getString(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.TINYINT) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
                        obj.put(columnName, rs.getDate(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
                        obj.put(columnName, rs.getTimestamp(i));
                    } else {
                        obj.put(columnName, rs.getObject(i));
                    }

                }
                respJSON.put(obj);
                //respJSON.add(obj);

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        respJSON.toString();

        return respJSON;

        System.out.print(respJSON.toString());
    }

My problem is that I can not print the json string to see it in the console ...

我的问题是我无法打印 json 字符串以在控制台中查看它...

I have tried the respJSON.toString(); and it doesnt seem to work ..

我试过 respJSON.toString(); 它似乎不起作用..

Could you please help me ? ...

请你帮助我好吗 ?...

Thank you

谢谢

采纳答案by Muhammad Saifuddin

Your program cannot reach to the printstatement as there is a returnstatement before that. If you had been using an IDE like Netbeansor Eclipse, that would have given you a clear warning.

您的程序无法访问该print语句,因为return在此之前有一个语句。如果您一直在使用 IDE 之类的NetbeansEclipse,那会给您一个明确的警告。

回答by bpgergo

this is a simple typo. instead of this:

这是一个简单的错字。而不是这个:

    respJSON.toString();
    return respJSON;
    System.out.print(respJSON.toString());

do this

做这个

    System.out.print(respJSON.toString());
    return respJSON;