java - 如何从数据库中检测 JSON 列类型

标签 java json postgresql

有一些代码可以将数据库结果集转换为 JSON。现在,它处理数值和字符串,并且工作得很好。但是,当它得到一个JSON值时,它在上面使用了toString,而不是把它当成JSON处理,这意味着在提取时,它已经转义了双引号等,无法重新序列化成JSON.

如何从 PostgreSQL 数据库中检测 JSON 列类型?

编辑:这是@f1sh 所建议的解决方案。 @f1sh,如果您想提交您的评论作为答案,我会将其标记为已接受的解决方案。

private JsonNode resultSetToJson(ResultSet rs) throws SQLException {

    ObjectNode node = DataFileTool.OBJECT_MAPPER.createObjectNode();

    ResultSetMetaData mData = rs.getMetaData();
    int columnCount = mData.getColumnCount();

    for (int i = 1; i <= columnCount; i++) {

        Object obj = rs.getObject(i);
        String columnName = mData.getColumnLabel(i);
        String columnNameToUse = dbColumnNameToJsonKey.containsKey(columnName)
                ? dbColumnNameToJsonKey.get(columnName) : columnName;

        if (mData.getColumnType(i) == Types.NUMERIC) {
            if (obj == null) {
                node.putNull(columnNameToUse);
            } else if (obj instanceof BigDecimal) {
                node.put(columnNameToUse, (BigDecimal) obj);
            } else if (obj instanceof Integer) {
                node.put(columnNameToUse, (Integer) obj);
            } else if (obj instanceof Double) {
                node.put(columnNameToUse, (Double) obj);
            } else if (obj instanceof Long) {
                node.put(columnNameToUse, (Long) obj);
            } else if (obj instanceof Float) {
                node.put(columnNameToUse, (Float) obj);
            } else if (obj instanceof Short) {
                node.put(columnNameToUse, (Short) obj);
            } else if (obj instanceof BigInteger) {
                node.set(columnNameToUse, BigIntegerNode.valueOf((BigInteger) obj));
            } else if (obj instanceof Byte) {
                node.put(columnNameToUse, (Byte) obj);
            } else {
                node.put(columnNameToUse, obj.toString());
            }
        } else if (obj instanceof PGobject) {
            PGobject pg = (PGobject) obj;

            if (pg.getType().equalsIgnoreCase("json")) {
                JsonNode pgNode;

                try {
                    pgNode = DataFileTool.OBJECT_MAPPER.readTree(pg.getValue());
                    node.set(columnNameToUse, pgNode);
                } catch (IOException e) {
                    LOGGER.error("An exception occurred while attempting to parse JSON PGObject to JsonNode: {}",
                            obj, e);
                    node.put(columnNameToUse, obj.toString());
                }
            } else {
                node.put(columnNameToUse, obj.toString());
            }
        } else if (obj == null) {
            node.put(columnNameToUse, StringUtils.EMPTY);
        } else {
            node.put(columnNameToUse, obj.toString());
        }
    }

    return node;
}

最佳答案

if (obj instanceof PGobject) {
        PGobject pg = (PGobject) obj;

        if (pg.getType().equalsIgnoreCase("json")) {
            JsonNode pgNode;

            try {
                pgNode = DataFileTool.OBJECT_MAPPER.readTree(pg.getValue());
                node.set(columnNameToUse, pgNode);
            } catch (IOException e) {
                LOGGER.error("An exception occurred while attempting to parse JSON PGObject to JsonNode: {}",
                        obj, e);
                node.put(columnNameToUse, obj.toString());
            }
        } else {
            node.put(columnNameToUse, obj.toString());
        }
    }

关于java - 如何从数据库中检测 JSON 列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180180/

相关文章:

java - 安卓开发: Line Spacing With Line Numbering

java - 如果 SSLSocket.startHandshake() 是同步的,为什么它会提供回调?

java - hibernate 实体到json

php表单通过基本认证restful api提交

在 PostgreSQL 上使用 LIKE 的 Django 原始 SQL 查询

java - 如何将 jsonNodes 列表转换为单个 jsonNode

java - 如何在java中准备带有可选子句的SQL查询?

javascript - 如何编写sql语句从web服务获取数据

sql - 尝试使用 CASE WHEN THEN 进行 GROUP BY - postgresql

postgresql - PyCharm PostgreSQL 方言检测