繁体   English   中英

将javax响应类型中包含的值转换为要格式化为json数组的列表

[英]Translating values contained in a javax Response type to a list to be formatted into a json array

所以我的问题对你们中的某些人可能有点愚蠢,但是我要查询一些必须作为响应返回的数据,然后我必须在应用程序的前端使用部分数据以使用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,749106 ,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], 787264],[1487620018508、787264],[1487619994967、787264],[1487619973549、778740],[1487619950069、770205],[1487619926850、749106],[1487619902486、740740],[1487619877298、728184]]

然后,我可以将其放入json中(至少我认为是这样!还没走那么远)。 但是这段代码看起来很荒谬,脆弱,并且不是正确的解决方法。

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

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

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

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

这里取

因此,您可以使用元数据属性获取所需的列,然后最好的方法是使用DTO对象存储每一行​​。 选中此项以了解有关DTO的更多信息

因此,基本上的想法是,您可以使用检索到的数据或当时需要从数据库中获得的数据来构建对象,并且可以使用通用的getter和setter来访问所有字段

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

希望能帮助到你

暂无
暂无

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

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