typescript 如何显示对象名称以数字开头的Json数据

h5qlskok  于 2022-11-18  发布在  TypeScript
关注(0)|答案(3)|浏览(112)
"current": {
            "observation_time": "03:38 PM",
            "temperature": 18,
            "weather_code": 113,
            "weather_icons": [
                "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
            ],
            "weather_descriptions": [
                "Sunny"
            ],
            "wind_speed": 0,
            "wind_degree": 345,
            "wind_dir": "NNW",
            "pressure": 1011,
            "precip": 0,
            "humidity": 58,
            "cloudcover": 0,
            "feelslike": 18,
            "uv_index": 5,
            "visibility": 16
        },
        "forecast": {
            "2019-09-07": {
                "date": "2019-09-07",
                "date_epoch": 1567814400,
                "astro": {
                    "sunrise": "06:28 AM",
                    "sunset": "07:19 PM",
                    "moonrise": "03:33 PM",
                    "moonset": "12:17 AM",
                    "moon_phase": "First Quarter",
                    "moon_illumination": 54
                },
                "mintemp": 17,
                "maxtemp": 25,
                "avgtemp": 21,
                "totalsnow": 0,
                "sunhour": 10.3,
                "uv_index": 5,

<!-- end snippet -->

我想在预报对象下显示日出和日落以及太阳时,到目前为止我已经尝试了{forecast[“2019-09-07”].astro.sunrise},但它仍然不起作用,请告诉我还有什么可以从api中调用它??提前感谢。

olhwl3o2

olhwl3o21#

使用randa路径https://ramdajs.com/docs/#path

path(['2019-09-07', 'astro', 'sunrise'])(forecast)
mepcadol

mepcadol2#

所以我做了

{country.forecast?.["2022-11-11"]?.astro?.sunrise}

而且它起作用了,还是谢谢你

idfiyjo8

idfiyjo83#

请尝试以下内容

{forecast['2019-09-07'].astro.sunrise}

谢谢

相关问题