访问和打印嵌套的JSON对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了访问和打印嵌套的JSON对象相关的知识,希望对你有一定的参考价值。

我在到达以下对象中的嵌套键时遇到麻烦。我正在尝试打印“ close_approach_data”内“ relative_velocity”内的所有“ kilometers_per_second”。

我返回一个错误,但不明白我在做什么错。预先谢谢你

const asteroidData = near_earth_objects
        .map((data) => data.close_approach_data[0])
        .map((e) => e.relative_velocity); 

// TypeError: Cannot read property 'relative_velocity' of undefined

or

const asteroidData = near_earth_objects
          .map((data) => data.close_approach_data[0].relative_velocity);

// TypeError: Cannot read property 'relative_velocity' of undefined

JSON对象:

 "near_earth_objects": [
        {
            "links": {
                "self": "http://www.neowsapp.com/rest/v1/neo/2021277?api_key=24TE7EgNfmXIvdb6vNNZGBWx8s54XbZzCCi2oAdN"
            },
            "id": "2021277",
            "neo_reference_id": "2021277",
            "name": "21277 (1996 TO5)",
            "designation": "21277",
            "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2021277",
            "absolute_magnitude_h": 16.1,
            "estimated_diameter": {
                "kilometers": {
                    "estimated_diameter_min": 1.6016033798,
                    "estimated_diameter_max": 3.5812940302
                },
                "meters": {
                    "estimated_diameter_min": 1601.6033797856,
                    "estimated_diameter_max": 3581.2940301941
                },
                "miles": {
                    "estimated_diameter_min": 0.9951898937,
                    "estimated_diameter_max": 2.2253122528
                },
                "feet": {
                    "estimated_diameter_min": 5254.6044325359,
                    "estimated_diameter_max": 11749.652706022
                }
            },
            "is_potentially_hazardous_asteroid": false,
            "close_approach_data": [
                {
                    "close_approach_date": "1945-06-07",
                    "close_approach_date_full": "1945-Jun-07 22:35",
                    "epoch_date_close_approach": -775272300000,
                    "relative_velocity": {
                        "kilometers_per_second": "15.5094751879",
                        "kilometers_per_hour": "55834.1106763388",
                        "miles_per_hour": "34693.1450477507"
                    },
                    "miss_distance": {
                        "astronomical": "0.0334232973",
                        "lunar": "13.0016626497",
                        "kilometers": "5000054.084456751",
                        "miles": "3106889.5396991238"
                    },
                    "orbiting_body": "Mars"
                }
            ],
            "is_sentry_object": false
        },

       // Object continues repeating the above structure
答案

您需要所有值还是仅需要kilometers_per_second值?

let near_earth_objects =  [
        {
            "links": {
                "self": "http://www.neowsapp.com/rest/v1/neo/2021277?api_key=24TE7EgNfmXIvdb6vNNZGBWx8s54XbZzCCi2oAdN"
            },
            "id": "2021277",
            "neo_reference_id": "2021277",
            "name": "21277 (1996 TO5)",
            "designation": "21277",
            "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2021277",
            "absolute_magnitude_h": 16.1,
            "estimated_diameter": {
                "kilometers": {
                    "estimated_diameter_min": 1.6016033798,
                    "estimated_diameter_max": 3.5812940302
                },
                "meters": {
                    "estimated_diameter_min": 1601.6033797856,
                    "estimated_diameter_max": 3581.2940301941
                },
                "miles": {
                    "estimated_diameter_min": 0.9951898937,
                    "estimated_diameter_max": 2.2253122528
                },
                "feet": {
                    "estimated_diameter_min": 5254.6044325359,
                    "estimated_diameter_max": 11749.652706022
                }
            },
            "is_potentially_hazardous_asteroid": false,
            "close_approach_data": [
                {
                    "close_approach_date": "1945-06-07",
                    "close_approach_date_full": "1945-Jun-07 22:35",
                    "epoch_date_close_approach": -775272300000,
                    "relative_velocity": {
                        "kilometers_per_second": "15.5094751879",
                        "kilometers_per_hour": "55834.1106763388",
                        "miles_per_hour": "34693.1450477507"
                    },
                    "miss_distance": {
                        "astronomical": "0.0334232973",
                        "lunar": "13.0016626497",
                        "kilometers": "5000054.084456751",
                        "miles": "3106889.5396991238"
                    },
                    "orbiting_body": "Mars"
                }
            ],
            "is_sentry_object": false
        }];
        
let result = near_earth_objects.flatMap(({close_approach_data }) => close_approach_data.map(({relative_velocity})=> Object.values(relative_velocity)).flat());

console.log(result);

以上是关于访问和打印嵌套的JSON对象的主要内容,如果未能解决你的问题,请参考以下文章

如何按名称过滤键,然后使用 jq 访问嵌套对象

使用python打印嵌套在数组中的json对象

使用嵌套片段和动画对象

如何访问和处理嵌套对象、数组或 JSON?

如何访问和处理嵌套对象、数组或 JSON?

如何访问和处理嵌套对象、数组或 JSON?