【问题标题】:Translating values contained in a javax Response type to a list to be formatted into a json array将 javax 响应类型中包含的值转换为要格式化为 json 数组的列表
【发布时间】:2017-02-20 19:59:45
【问题描述】:

所以我的问题对你们中的一些人来说可能有点傻,但我正在查询一些必须作为响应返回的数据,然后我必须在我的应用程序的前端使用部分数据来绘制它使用 AngularJS 和 nvD3 图表。为了正确格式化绘图工具的数据,我必须将这些数据转换为正确的 json 格式。我找不到直接的方法来从返回的响应中提取我需要的数字。我只需要获取我需要的值并将它们转换为一个列表,然后再解析为一个 json 数组。以下是我的解决方法,它有效,给了我正在寻找的列表......

if (tableState.getIdentifier().getProperty().equals("backupSize")){
        Response test4 = timeSeriesQuery.queryData("backup.data.size,", "", "1y-ago", "25", "desc");
        String test5 = test4.getEntity().toString();
        int test6 = test5.indexOf("value");
        int charIndexStart = test6 + 9;
        int charIndexEnd = test5.indexOf(",", test6);
        String test7 = test5.substring(charIndexStart, charIndexEnd);
        int charIndexStart2 = test5.indexOf(",", charIndexEnd);
        int charIndexEnd2 = test5.indexOf(",", charIndexStart2 + 2);
        String test9 = test5.substring(charIndexStart2 + 1, charIndexEnd2);
        long test8 = Long.parseLong(test7);
        long test10 = Long.parseLong(test9);
        List<Long> graphs = new ArrayList<>();
        graphs.add(test8);
        graphs.add(test10);
        List<List<Long>> graphs2 = new ArrayList<List<Long>>();
        graphs2.add(graphs);
        for(int i=1, charEnd = charIndexEnd2; i<24; i++){
            int nextCharStart = test5.indexOf("}", charEnd) + 2;
            int nextCharEnd = test5.indexOf(",", nextCharStart);
            String test11 = test5.substring(nextCharStart + 1, nextCharEnd);
            int nextCharStart2 = test5.indexOf(",", nextCharEnd) + 1;
            int nextCharEnd2 = test5.indexOf(",", nextCharStart2 + 2);
            String test13 = test5.substring(nextCharStart2, nextCharEnd2);
            long test12 = Long.parseLong(test11);
            long test14 = Long.parseLong(test13);
            List<Long> graphs3 = new ArrayList<>();
            graphs3.add(test12);
            graphs3.add(test14);
            graphs2.add(graphs3);
            charEnd = test5.indexOf("}", nextCharEnd2);
        } return graphs2;

这是test5的结果:

xxx.xx.xxxxxx.entity.timeseries.datapoints.queryresponse.DatapointsResponse@2be02a0c[start=, end=, tags={xxx.xx.xxxxxx.entity.timeseries.datapoints.queryresponse.Tag@1600cd19[name= backup.data.size, results={xxx.xx.xxxxxx.entity.timeseries.datapoints.queryresponse.Results@2b8a61bd[groups={xxx.xx.xxxxxx.entity.timeseries.datapoints.queryresponse.Group@61540dbc[name= type, type=number]}, attributes=xxx.xx.xxxxxx.entity.util.map.Map@4b4eebd0[], values={{1487620485896,973956,3},{1487620454999,973806,3},{1487620424690, 956617,3},{1487620397181,938677,3},{1487620368825,934494,3},{1487620339219,926125,3},{1487620309050,917753,3},{1487620279239,909384,3},{1487620251381,872864, 3},{1487620222724,846518,3},{1487620196441,832150,3},{1487620168141,819563,3},{1487620142079,787264,3},{1487620115827,787264,3},{1487620091991,787264,3} ,{1487620067230,787264,3},{1487620042333,787264,3},{1487620018508,787264,3},{1487619994967,787264,3},{1487619973549,778740,3},{1487619950069,770205,3},{ 1487619926850,74910 6,3},{1487619902486,740729,3},{1487619877298,728184,3},{1487619851449,719666,3}}]},stats=xxx.xx.xxxxxx.entity.timeseries.datapoints.queryresponse.Stats@ 5bb68fa5[rawCount=25]]}]

以及返回的列表:

[[1487620485896, 973956], [1487620454999, 973806], [1487620424690, 956617], [1487620397181, 938677], [1487620368825, 934494], [1487620339219, 926125], [1487620309050, 917753], [1487620279239, 909384 ], [1487620251381, 872864], [1487620222724, 846518], [1487620196441, 832150], [1487620168141, 819563], [1487620142079, 787264], [1487620115827, 787264], [1487620091991, 787264], [1487620067230, 787264], [1487620042333, 787264], [1487620018508, 787264], [1487619994967, 787264], [1487619973549, 778740], [1487619950069, 770205], [1487619926850, 749106], [1487619902486, 740729], [1487619877298, 728184]]

然后我可以把它塞进一个 json 中(至少我是这么认为的!还没有那么远)。但是这段代码看起来很荒谬,很脆弱,而且不是解决这个问题的正确方法。

有没有人有更好的方法从响应中提取数据点并将它们转换为 json 数组或至少是嵌套列表?

感谢所有阅读的人,如果我可以提供更多信息,请告诉我。

【问题讨论】:

  • 这些数据是否来自数据库?
  • 是的,是@Steven
  • 好的,那你需要使用resultset,我来开发一个答案

标签: java arrays json


【解决方案1】:

当我们只需要查询中的几个值时,检索它们的最佳方法是使用 resultSet 进行查询并使用它强大的元数据:

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
 ResultSetMetaData rsmd = rs.getMetaData();
 String name = rsmd.getColumnName(1);

取自here

因此,您可以使用元数据属性获取所需的列,然后您能做的最好的事情就是使用 DTO 对象来存储每一行​​ check this 以了解有关 DTO 的更多信息

所以,基本上这个想法是你从你检索到的数据或者只是你当时从数据库中需要的数据构建一个对象,你可以使用常见的 getter 和 setter 来访问所有字段

但是,在收集数据时,您通常会使用循环,因为您需要遍历 resultSet 值以询问列的名称并在一致时保留其值。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多