【问题标题】:parsing json response that has multiple keys using flutter使用颤振解析具有多个键的json响应
【发布时间】:2020-09-19 17:48:58
【问题描述】:

我正在尝试从这个 json 响应中获取这些键的值:

{
    "wlan": {
        "channel": 1,
        "ssid": "WLAN-25UR7J",
        "mac": "00:17:91:80:22:96",
        "inet": [
            {
                "netmask": "255.255.255.0",
                "ip": "192.168.2.112",
                "family": "IPv4"
            }
        ],
        "stationinfo": {
            "signal": -48,
            "channelwidth": 0,
            "bitrate": 72.2
        },
        "mode": "station",
        "encryption": "WPA2-PSK"
    }
}

我可以使用这个方法从 wlan key 中获取值。

json.decode(response.body)['wlan']['channel']

但我没有处理其他值,例如获取网络掩码或比特率。

【问题讨论】:

  • 您是否尝试过使用json.decode(response.body)['wlan']['inet'][0]['netmask']
  • 非常感谢,不,我没有,它适用于 inet key ,但是对于 stationinfo json.decode(response.body)['wlan']['inet'][1] ['signal'] 或 json.decode(response.body)['wlan']['inet']['stationinfo'][0]['signal']
  • ['wlan']['stationinfo']['signal'] 效果很好,再次感谢

标签: json flutter http dictionary dart


【解决方案1】:

对于网络掩码,您可以这样访问它:

json.decode(response.body)['wlan']['inet'][0]['netmask']

对于比特率,您可以通过以下方式访问它:

json.decode(response.body)['wlan']['stationinfo']['bitrate']

如果任何键中的值是一个数组,则可以通过像value[0] 这样的索引访问它们,或者您可以简单地迭代数组并执行必要的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 2019-01-17
    • 2022-01-20
    • 2021-10-27
    • 2021-09-28
    • 2020-11-22
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多