无法解析Groovy中包含大写键的JSON

时间:2017-08-15 20:48:33

标签: json rest groovy escaping

我试图从REST API解析JSON,并且当该数据的键是大写格式时无法访问数据。

{
    "body": {
        "devices": [{
            "_id": "xxxxxxxxxx",
            "cipher_id": "xxxxxxxx",
            "last_status_store": 1502808369,
            "modules": [{
                "_id": "xxxxxxx",
                "type": "xxxxxxx",
                "last_message": 1502808365,
                "last_seen": 1502808359,
                "dashboard_data": {
                    "time_utc": 1502808359,
                    "Temperature": 18.9,
                    "temp_trend": "down",
                    "Humidity": 27,
                    "date_max_temp": 1502804720,
                    "date_min_temp": 1502808359,
                    "min_temp": 18.9,
                    "max_temp": 22.2
                },
                "data_type": [
                    "Temperature",
                    "Humidity"
                ],
                "last_setup": 1502731328,
                "battery_vp": 6354,
                "battery_percent": 100,
                "rf_status": 67,
                "firmware": 44
            }],
            "place": {
                "altitude": 63.395306309052,
                "city": "xxxxxx",
                "country": "US",
                "timezone": "America/New_York",
                "location": [-72.532673,
                    42.0425917
                ]
            },
            "station_name": "xxxxxxxxxxx",
            "type": "NAMain",
            "dashboard_data": {
                "AbsolutePressure": 1004.6,
                "time_utc": 1502808354,
                "Noise": 50,
                "Temperature": 22.7,
                "temp_trend": "up",
                "Humidity": 69,
                "Pressure": 1012.1,
                "pressure_trend": "stable",
                "CO2": 0,
                "date_max_temp": 1502808290,
                "date_min_temp": 1502801263,
                "min_temp": 21.3,
                "max_temp": 22.7
            },
            "data_type": [
                "Temperature",
                "CO2",
                "Humidity",
                "Noise",
                "Pressure"
            ],
            "co2_calibrating": false,
            "date_setup": 1502731277,
            "last_setup": 1502731277,
            "module_name": "Indoor",
            "firmware": 132,
            "last_upgrade": 1502731279,
            "wifi_status": 51
        }]
    },
    "status": "ok",
    "time_exec": 0.019752025604248,
    "time_server": 1502808443
}

我正在尝试使用Groovy Json slurper访问JSON并执行以下命令。我的调试器中的响应很好..Reponse.content是我发送到服务器的HTTPrequest的值。

def stationInfo = jsonSlurper.parseText(response.content as String)
def outsideTemp = stationInfo.body.devices.modules.dashboard_data.Temperture    
def outsideHumidty = stationInfo.body.devices.modules.dashboard_data.Humidty    
def insideTemp = stationInfo.body.devices.dashboard_data.Temperture

outsideTemp,outsideHumidty和insideTemp都是eqaull到" [null]"当我在调试器中查看它们时。关于为什么会发生这种情况的任何想法,以及如何解决这个问题?编译器是否假设某些东西,因为它们是大写的?

1 个答案:

答案 0 :(得分:1)

看起来你差不多了。

请注意,您附加的json似乎无效,已修复以便能够正常工作。

def pJson = new groovy.json.JsonSlurper().parseText(response.content as String)
println pJson.body.devices.modules.dashboard_data.Temperature.flatten()
println pJson.body.devices.dashboard_data.Temperature.flatten()

Temparature类似,您也可以将其用于Humidity

您可以在 demo

中看到相同的内容