迭代JSON数据以生成表Groovy

时间:2017-01-25 21:20:08

标签: json api groovy

我为import.io创建了一个API包装器,它将一些数据带回来。我希望创建一个数据表,以便在我的网站上显示足球联赛。我很难看到如何迭代数据并将其转换为Groovy中的表。我目前正在使用JsonSlurper将API中的数据转换为JSON。这是我的JSON

{
    "extractorData": {
        "data": [
            {
                "group": [
                    {
                        "Content": [
                            {
                                "text": "Clapham Junction Thursday"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "P"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "W"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "D"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "L"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "GF"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "GA"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "GD"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "PTS"
                            }
                        ]
                    },
                    {
                        "Content": [
                            {
                                "text": "Wavey Wanderers"
                            }
                        ],
                        "Link": [
                            {
                                "href": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/TeamProfile.aspx?TeamId=2865&LeagueId=28&SeasonId=61&DivisionId=1098",
                                "text": "Wavey Wanderers"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "5"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "3"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "2"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "31"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "22"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "9"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "9"
                            }
                        ]
                    },
                    {
                        "Content": [
                            {
                                "text": "The Feet of Hermes"
                            }
                        ],
                        "Link": [
                            {
                                "href": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/TeamProfile.aspx?TeamId=2206&LeagueId=28&SeasonId=61&DivisionId=1098",
                                "text": "The Feet of Hermes"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "5"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "3"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "2"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "21"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "13"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "8"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "9"
                            }
                        ]
                    },
                    {
                        "Content": [
                            {
                                "text": "Multiple Scoregasms"
                            }
                        ],
                        "Link": [
                            {
                                "href": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/TeamProfile.aspx?TeamId=959&LeagueId=28&SeasonId=61&DivisionId=1098",
                                "text": "Multiple Scoregasms"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "5"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "3"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "2"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "25"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "24"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "1"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "9"
                            }
                        ]
                    },
                    {
                        "Content": [
                            {
                                "text": "Pugh let the Cruyffs out"
                            }
                        ],
                        "Link": [
                            {
                                "href": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/TeamProfile.aspx?TeamId=2875&LeagueId=28&SeasonId=61&DivisionId=1098",
                                "text": "Pugh let the Cruyffs out"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "5"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "3"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "2"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "17"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "20"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "-3"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "9"
                            }
                        ]
                    },
                    {
                        "Content": [
                            {
                                "text": "Michu At De Gea Ba"
                            }
                        ],
                        "Link": [
                            {
                                "href": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/TeamProfile.aspx?TeamId=2656&LeagueId=28&SeasonId=61&DivisionId=1098",
                                "text": "Michu At De Gea Ba"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "5"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "2"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "3"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "20"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "20"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "6"
                            }
                        ]
                    },
                    {
                        "Content": [
                            {
                                "text": "Shire Soldiers"
                            }
                        ],
                        "Link": [
                            {
                                "href": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/TeamProfile.aspx?TeamId=1994&LeagueId=28&SeasonId=61&DivisionId=1098",
                                "text": "Shire Soldiers"
                            }
                        ],
                        "Value 1": [
                            {
                                "text": "5"
                            }
                        ],
                        "Value 2": [
                            {
                                "text": "1"
                            }
                        ],
                        "Value 3": [
                            {
                                "text": "0"
                            }
                        ],
                        "Value 4": [
                            {
                                "text": "4"
                            }
                        ],
                        "Value 5": [
                            {
                                "text": "17"
                            }
                        ],
                        "Value 6": [
                            {
                                "text": "32"
                            }
                        ],
                        "Value 7": [
                            {
                                "text": "-15"
                            }
                        ],
                        "Value 8": [
                            {
                                "text": "3"
                            }
                        ]
                    }
                ]
            }
        ],
        "resourceId": "7103552b590f38e1b6c50352145431ac",
        "url": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/Standings.aspx?LeagueId=28&SeasonId=61"
    },
    "pageData": {
        "resourceId": "7103552b590f38e1b6c50352145431ac",
        "statusCode": 200,
        "timestamp": 1485378614012
    },
    "runtimeConfigId": "cfc5c9d6-2799-4eec-a899-d18fbd53db97",
    "sequenceNumber": -1,
    "timestamp": 1485378824648,
    "url": "http://london5aside.spawtz.com/SpawtzSkin/Fixtures/Standings.aspx?LeagueId=28&SeasonId=61"
}

以下是我目前在Ratpack处理程序中执行的操作

class LeagueTableHandler extends InjectionHandler {
    void handle(Context context, ImportClient importClient) {
        def data = importClient.leagueDataExtractor()
        def jsonSlurper = new JsonSlurper().parseText(data.contentAsString)
        context.render Jackson.json(jsonSlurper)
    }
}

理想情况下,我希望它看起来像这样

json screenshot 在这样处理Json时,有没有人有任何提示?

修改

我不确定它的JSON结构是否会导致我出现问题,但我在测试中设法获取值的唯一方法就是这样做。

 List<String> teamNames = []

        when:
         jsonSlurped.result.extractorData.data.group.Content.text.each { firstNested ->
            firstNested.each { secondNested ->
                secondNested.each { finalValue ->
                    teamNames.add(finalValue)
                }
            }
        }

显然,我宁愿不为价值1,价值2等做这件事,是否有更好的方法来获取每一条信息?

0 个答案:

没有答案