【问题标题】:Convert relational data into JSON Efficiently有效地将关系数据转换为 JSON
【发布时间】:2015-07-30 13:59:57
【问题描述】:

我有下表People

我想获取关系记录并使 JSON Array 如下所示:

{ "Person":[
    {"ID":1, 
     "FirstName":"James", 
     "LastName":"Donovan",
     "Child":[
         {"ID":6, "FirstName":"Nikolai", "LastName":"Donovan"}
     ]
    },
    {"ID":2, 
     "FirstName":"Jeffrey", 
     "LastName":"Williams",
     "Child":[
         {"ID":4, "FirstName":"Carol", "LastName":"Williams"},
         {"ID":5, "FirstName":"Sarah", "LastName":"Williams"}
     ]
    },
    .... and so on
  ]
}

我使用以下方法; 以下代码的简短摘要:我在循环中执行了两次 db 操作并将节点放入 JSON 对象中匹配某些条件。

preparedStatement = con.prepareStatement("SELECT * FROM PEOPLE");
rs = preparedStatement.executeQuery(); // ResultSet Object
            
if (rs != null && rs.next()) {
    Roles = new JSONObject();
    parentNode = new JSONObject();
    childNode = new JSONObject();
            
    while(rs.next()) {
        parentNode.put("ID", rs.getInt("Id"));
        parentNode.put("FirstName", rs.getString("firstname"));
        parentNode.put("LastName", rs.getString("lastname"));

        if(rs.getInt("parent") > 0) { // Equal 0 Mean it has not child
            // Perform again database operation and get record related to parent id
            preparedStatement = con.prepareStatement("SELECT * FROM PEOPLE WHERE parent=?");
            preparedStatement.setString(1, rs.getInt("id");
            resultSet2 = preparedStatement.executeQuery();
            
            while(resultSet2.next()) {
                childNode.put("ID", rs.getInt("Id"));
                childNode.put("FirstName", rs.getString("firstname"));
                childNode.put("LastName", rs.getString("lastname"));
                
                parentNode.put("Child":childRole); // put child into parent node
            }
       }
    }
}

我想要什么? 在循环中执行 db select 查询对于服务器端来说太昂贵了

有没有更好的解决方案可以生成所需的关系数据 JSON 并让我免于额外的 SELECT 操作!

感谢您的关注!

【问题讨论】:

  • 你能把所有的People 放在Map 中,使用id 作为键吗?如果记录的数量不是太多,这将为您节省一些资源。

标签: java mysql json jakarta-ee


【解决方案1】:

如果您使用的是 mysql 5.7,您可以使用 JSON_ARRAY 函数来创建 json 数组。或 JSON_OBJECT https://dev.mysql.com/doc/refman/5.7/en/json-attribute-functions.html

【讨论】:

    【解决方案2】:

    也许使用两个查询。

    1. 选择 blah,其中 parent = 0。
    2. select blah where parent != 0 order by parent, id

    循环遍历第二个结果集的结果并将它们分配给第一个结果集中的父级。

    使用 gson(或 jackson 或您喜欢的任何 json 库)将对象列表转换为最终的 jason。

    类似代码的简短内容

    select the parents (parent = 0).
    for each parent row, add the row to a Map<String, Entity> where the key is the parent id.
    
    select the children (parent != 0).
    for each child row, get the parent row from the map using the parent id then add them to the parent entity.
    

    对父行和子行使用相同的类,将子集合初始化为 null 并在第一次添加子时创建它。这样,有孩子的父母会包含孩子数组,而没有孩子的父母和孩子不会包含孩子数组。

    【讨论】:

      【解决方案3】:

      你可以发出一个单一的选择

      SELECT p.ID, p.FirstName, p.LastName, c.ID, c.FirstName, c.LastName 
      FROM PEOPLE p LEFT OUTER JOIN PEOPLE c ON c.parent = p.ID
      ORDER BY p.ID
      

      然后循环遍历结果。 每当父 ID 更改(以及第一个结果行)时,您都会构建一个新的当前父节点。然后读取子数据(如果父节点没有子节点,可能为空)并放入当前父节点的子json数组中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-11
        • 2015-11-15
        • 2019-01-28
        • 2012-08-07
        • 2018-04-08
        • 1970-01-01
        • 2021-07-27
        相关资源
        最近更新 更多