【问题标题】:Unable to parse JSON that contains upper case key in Groovy无法在 Groovy 中解析包含大写键的 JSON
【发布时间】:2017-08-15 20:48:32
【问题描述】:

我正在尝试从 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 当我在调试器中查看它们时都等于“[null]”。关于为什么会发生这种情况以及如何解决它的任何想法?编译器是否假设某些内容是因为它们是大写的?

【问题讨论】:

  • 我们是否假设dashboard_data 中的其他元素被正确返回? (如果可以,请在您的问题中明确说明。)
  • 可能问题在于devices 是一个数组,应该由[index] 访问,而您将其视为普通对象
  • "modules" 同上
  • 所有内容都在 json 中正确返回,我无法通过以这种方式引用其他字段来访问其他字段(普通对象)
  • 另外需要注意的是,这些大写的值在我引用它们时会突出显示语法,但如果保留小写,则不会

标签: json rest groovy escaping


【解决方案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中看到同样的内容

【讨论】:

  • 非常感谢。我还拼错了温度和湿度,这没有帮助。
猜你喜欢
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
相关资源
最近更新 更多