如何从Groovy中的字符串获取键集?

时间:2018-08-29 07:22:29

标签: groovy

我在Groovy中有以下字符串:

Sub Diagramm_OV()

    Dim a As Integer

    a = 1
    Do
        ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
        With ActiveChart
            .SeriesCollection.NewSeries
            .FullSeriesCollection(1).XValues = "='Project Overview'!$E$8"
            .FullSeriesCollection(1).Values = "={1}"
            .Axes(xlCategory).Select
            .Axes(xlCategory).MaximumScale = 100
        End With

        ActiveChart.ChartTitle.Select
        Selection.Delete
        ActiveChart.Axes(xlValue).Select
        Selection.Delete
        ActiveChart.Axes(xlValue).MajorGridlines.Select
        Selection.Delete
        ActiveChart.PlotArea.Select

        With ActiveChart.PlotArea.Format.Fill
            .Visible = msoTrue
            .TwoColorGradient msoGradientVertical, 1
            .ForeColor.RGB = RGB(255, 0, 0)
            .BackColor.RGB = RGB(0, 255, 0)
            .GradientStops(1).Position = 0
            .GradientStops(2).Position = 1
        End With

        With ActiveSheet
            .Shapes("Diagramm " & a).ScaleHeight 0.3361111111, msoFalse, _
            msoScaleFromTopLeft
            .Shapes("Diagramm " & a).IncrementLeft 117.6
            .Shapes("Diagramm " & a).IncrementTop -107.4
            .ChartObjects("Diagramm " & a).Activate
            .Shapes.AddShape(msoShapePentagon, 150, 244.8, 24.6, 7.8).Select
        End With

        ' Create Pentagon and copy it
        Selection.ShapeRange.IncrementRotation 90
        Selection.Copy

        ActiveSheet.ChartObjects("Diagramm " & a).Activate

        ' Paste Pentagon to Scatter
        With ActiveChart
            .FullSeriesCollection(1).Select
            .Paste
        End With

        ActiveSheet.Shapes("Diagramm " & a).ScaleHeight 0.7685950413, msoFalse, _
            msoScaleFromTopLeft
        a = a + 2
    Loop Until a = 10
End Sub

我只需要获取键'qunit'和'utest'

怎么可能?

2 个答案:

答案 0 :(得分:2)

您可以解析该JSON字符串,然后阅读列表:

def p = new JsonSlurper()
def list = 
    p.parseText("""[[{"qunit":{"total":0,"passed":0,"failed":0,"skipped":0}}], 
                   [{"utest": {"total":0,"passed":0,"failed":0,"skipped":0}}]]""")

def keys = list.flatten().collect{it.keySet()}.flatten()

结果为[qunit, utest]

这显然是针对输入布局的。

答案 1 :(得分:2)

您的字符串代表一个JSON文档,因此您需要首先使用JsonSlurper来解析它:

import groovy.json.JsonSlurper

final String json = '[[{"qunit":{"total":0,"passed":0,"failed":0,"skipped":0}}], [{"utest": {"total":0,"passed":0,"failed":0,"skipped":0}}]]'

def list = new JsonSlurper().parseText(json)

如果您打印list变量,则会看到类似以下内容的信息:

[[[qunit:[total:0, passed:0, failed:0, skipped:0]]], [[utest:[total:0, passed:0, failed:0, skipped:0]]]]

首先,我们需要弄平列表:

list.flatten()

它返回一个列表,如:

[[qunit:[total:0, passed:0, failed:0, skipped:0]], [utest:[total:0, passed:0, failed:0, skipped:0]]]

折叠初始列表将产生List<Map<String, Object>>。我们可以使用扩展运算符*在列表中存储的每个地图上执行keySet()方法:

list.flatten()*.keySet()

这部分代码生成List<List<String>>类型的列表,例如:

[[qunit], [utest]]

最后,我们可以通过最后调用List<String>将其转换为flatten(),例如:

list.flatten()*.keySet().flatten()

应用上一个操作后,我们将得到一个列表,如:

[qunit, utest]

这是完整的示例:

import groovy.json.JsonSlurper

final String json = '[[{"qunit":{"total":0,"passed":0,"failed":0,"skipped":0}}], [{"utest": {"total":0,"passed":0,"failed":0,"skipped":0}}]]'

def list = new JsonSlurper().parseText(json)

def keys = list.flatten()*.keySet().flatten()

assert keys == ['qunit', 'utest']