简体   繁体   English

从ResultSet返回JSONArray或JSONObject

[英]Returning JSONArray or JSONObject from ResultSet

I have the following method... 我有以下方法...

I am trying to verify results in a oracle DB. 我正在尝试在oracle数据库中验证结果。 The result is not what I expect, but I cant seem to figure out how to make it right without doing a map, or list/map. 结果不是我所期望的,但是我似乎无法弄清楚如何在不制作地图或列表/地图的情况下正确进行制作。

public static JSONArray verifyDRFromButton(String transactionId) {
    JSONArray jsonArray = new JSONArray();
    try {
        ResultSet resultSet;
        Constants.threadSleep(5000);
        Constants.verifyOracle();
        String databaseConnectionString = String.format("jdbc:oracle:thin:@%s:%s:%s",  "*****", "****", "*****");

        Connection conn = DriverManager.getConnection(databaseConnectionString,"*****","*****");
        Statement stmt = conn.createStatement();
        resultSet = stmt.executeQuery("SELECT * from ****** WHERE ***** = '" + transactionId + "'");
        //Print DB Table
        Constants.threadSleep(500);
        //DBTablePrinter.printResultSet(rs);
        //result = rs.toString();
        while (resultSet.next()) {
            int totalRows = resultSet.getMetaData().getColumnCount();
            for (int i = 0; i < totalRows; i++) {
                JSONObject obj = new JSONObject();    
                obj.put(resultSet.getMetaData().getColumnLabel(i + 1)
                        .toLowerCase(), resultSet.getObject(i + 1));
                jsonArray.put(obj);
            }
        }
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jsonArray;
}

The result looks like this... 结果看起来像这样...

[
    {"id": "idvalue"}, 
    {"id2": "id2value"}, 
    {"name": "namevalue"}, 
    {"name2": "name2value"}
]

I would like it to be. 我希望如此。

[
    {"id": "idvalue", "id2": "id2value"}, 
    {"name": "namevalue", "name2": "name2value"}
]

Move this line 移动这条线

JSONObject obj = new JSONObject()

Before the for loop and this line 在for循环和此行之前

jsonArray.put(obj);

After for the loop If you do not get intended results please share your results 循环之后如果您没有获得预期的结果,请分享您的结果

First of all, try to use JSON-P ( https://javaee.github.io/jsonp/index.html ) which is the standard JSON specification in enterirpise Java, since Java EE 7. 首先,从Java EE 7开始,尝试使用JSON-P( https://javaee.github.io/jsonp/index.html ),这是enterirpise Java中的标准JSON规范。

Second, this 第二,这个

[
    {"id":, "idvalue"}, 
    {"id2":, "id2value"}, 
    {"name":, "namevalue"}, 
    {"name2":, "name2value"}
] 

is not a valid JsonArray, since the contained objects are not valid JSONs. 不是有效的JsonArray,因为所包含的对象不是有效的JSON。 So there must be something wrong with your algorithm. 因此,您的算法一定存在问题。

Third, if you use JSON-P, which is based on interfaces, you can add your implementation of JsonArray, based on the ResultSet: this means you can at least hide that procedural mess if not even refactor it and make it more elegant. 第三,如果您使用基于接口的JSON-P,则可以基于ResultSet添加JsonArray的实现:这意味着您至少可以隐藏该程序混乱,即使不进行重构也可以使其更加优雅。

The idea is to make the ResultSet act as a JsonArray, not to transform it into a JsonArray. 这个想法是使ResultSet 充当 JsonArray,而不是将其转换为JsonArray。 More about this topic, here: https://www.amihaiemil.com/2017/10/16/javaee8-jsoncollectors-oop-alternative.html 有关此主题的更多信息,请访问: https//www.amihaiemil.com/2017/10/16/javaee8-jsoncollectors-oop-alternative.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM