如何从数据库

时间:2016-02-03 14:48:51

标签: java json postgresql

有一些代码将数据库ResultSet转换为JSON。现在,它处理数值和字符串,并且工作正常和花花公子。但是,当它获得JSON值时,它会在其上使用toString,而不是将其视为JSON,这意味着在提取时,它已经转义了双引号等,并且无法重新序列化为JSON。 / p>

如何从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;
}

1 个答案:

答案 0 :(得分:0)

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());
        }
    }