java - 如何在 Eclipse 中使用 gson 将 ResultSet 查询转换为 JSON?

标签 java json jdbc gson resultset

我编写的代码是:-

import com.google.gson.Gson;
import com.google.gson.JsonObject;

 public class JsonFileCreation{
    public static JsonArray convertToJSON(ResultSet resultSet)
            throws Exception {
        JsonArray jsonArray = new JsonArray();
        while (resultSet.next()) {
            int total_columns = resultSet.getMetaData().getColumnCount();
            JsonObject obj = new JsonObject();
            for (int i = 0; i < total_columns; i++) {
                obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
            }
          jsonArray.put(obj);
        }
        return jsonArray;
    }
public static void main(String args[]) {
        Gson gson = new Gson(); 
        JsonArray jsonArr = new JsonArray();
        ....
        }

这显示了该行中的错误。它显示 put(String,Object) 没有为 Json 对象类型定义。

jsonArray.put(obj);

我的结果集查询是-

sql = "SELECT * FROM EMPLOYEE";
ResultSet rs = stmt.executeQuery(sql); 

表格如下所示:

Click here to view the table

我是初学者。请帮助我如何正确编写代码并在浏览器中获取 json 输出。

最佳答案

您收到的错误是:

put(String,Object) is not defined for the type JsonObject.

如果您查看 Gson Javadoc对于 JsonObject,该消息是正确的。 JsonObject 类没有 put 方法。

相反,有一些 add 方法,您可能会想要使用这些方法。

但是,没有 add 方法可以接受任何类型的对象并将其放入 JSON。您必须自己处理各种不同类型的值。您可能会得到 null 值、字符串、数字、日期以及可能的其他值。

我建议创建一个新方法,如下所示,来处理向 JSON 对象 obj 添加单个值。它会在它知道的几种不同类型中检查给定值,并使用相关的 JsonObject addaddProperty 方法来添加值:

    private static void addValueToJSON(JsonObject obj, String propertyName, Object value) throws Exception {
        if (value == null) {
            obj.add(propertyName, JsonNull.INSTANCE);
        } else if (value instanceof Number) {
            obj.addProperty(propertyName, (Number)value);
        } else if (value instanceof String) {
            obj.addProperty(propertyName, (String)value);
        } else if (value instanceof java.sql.Date) {
            // Not clear how you want dates to be represented in JSON.
            // Perhaps use SimpleDateFormat to convert them to a string?
            // I'll leave it up to you to finish this off.
        } else {
           // Some other type of value.  You can of course add handling
           // for extra types of values that you get, but it's worth
           // keeping this line at the bottom to ensure that if you do
           // get a value you are not expecting, you find out about it.
           throw new Exception("Unrecognised type of value: " + value.getClass().getName());
        }
    }

完成此操作后,您可以通过替换该行来调用新方法

                obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));

                addValueToJSON(obj, resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));

最后,您写道,您的错误发生在该行

jsonArray.put(obj);

我认为这是不正确的,因为在这一行中您没有尝试调用 JsonObject 上的方法。然而,JsonArray类也没有 put 方法,因此这一行也有错误。这种情况下的错误更容易修复:与 JsonObject 类一样,JsonArray 类也有 add 方法,但您可以使用一个采用 JsonElement 的方法,因为您要将 JsonObject 添加到数组,并且 JsonObject 扩展了 JsonElement。这次修复的只是替换的情况

jsonArray.put(obj);

jsonArray.add(obj);

关于java - 如何在 Eclipse 中使用 gson 将 ResultSet 查询转换为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62308001/

相关文章:

java - 使用扫描仪读取文件

java - 使用 D2RQ 映射数据库

java - 如何将GMetrics工具与Grails 3x应用程序集成

c# - 通过 C# 将 JSON 数据 POST 到 PHP 并将值存储在 MYSQL 中

java - JDBC 结果集类型滚动敏感

java - 使用 MySQL 连接器的 Java Servlet 中的 XML 响应缓慢

java - 在Android中为Imageview设置唯一的标签ID

javascript - 在each 函数中使用if/else 语句

javascript - 如何在数据格式的 ng-repeat 中使用 order 和 group by

ms-access - Microsoft Access 是否有 64 位驱动程序?