如何在 Eclipse 中使用 gson 将 ResultSet Query 转换为 JSON?

How to make convert ResultSet Query to JSON using gson in eclipse?

我写的代码是:-

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 Object.

定义
jsonArray.put(obj);

我的结果集查询是-

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

table 看起来像这样:

Click here to view the table

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

您得到的错误是:

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

如果您查看 JsonObjectGson Javadoc,该消息是正确的。 JsonObject class 没有 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 class 也没有 put 方法,所以这一行也有错误。这种情况下的错误更容易修复:与 JsonObject class 一样,JsonArray class 也有 add 方法,但您可以使用一个这需要一个 JsonElement,因为您要向数组添加一个 JsonObject,并且 JsonObject 扩展了 JsonElement。这次修复只是替换

的情况
jsonArray.put(obj);

jsonArray.add(obj);