【问题标题】:React Native - Error reading JSON Data properly (Type Error: undefined is not an object)React Native - 正确读取 JSON 数据时出错(类型错误:未定义不是对象)
【发布时间】:2021-01-05 16:25:26
【问题描述】:

目前,我正在处理一个学校项目,我们必须编写一个应用程序。我们需要从地址中获取纬度和经度。我找到了 Google API 和 HERE API,但是对于 Google API,您需要提供信用卡信息,所以我们不会使用它。

我已经环顾四周,但找不到任何可以帮助我解决这种情况的东西。

我的问题是从 responseJson 中读取数据。我收到此错误。

[Tue Jan 05 2021 17:16:18.172] LOG TypeError: undefined is not an object (evalating 'responseJson[0].ResponseView')::: Error Message

这是被调用的代码 sn-p

function setLocation(plz, town, street, hnr) {

    fetch('https://geocoder.ls.hereapi.com/6.2/geocode.json?apiKey={API-KEY}&searchtext=' + plz + '+' + town + '+' + street + '+' + hnr, {
        method: 'GET',
    })
        .then((response) => response.json())
        .then((responseJson) => {
            console.log(responseJson[0].ResponseView.Result.Location.DisplayPosition.Latitude);
        })
        .catch((error) => {
            console.log(error + "::: Error Message");
        });
}

这是我们调用链接时得到的数据

{
    "Response": {
        "MetaInfo": {
            "Timestamp": "2021-01-05T16:10:19.142+0000"
        },
        "View": [
            {
                "_type": "SearchResultsViewType",
                "ViewId": 0,
                "Result": [
                    {
                        "Relevance": 0.97,
                        "MatchLevel": "houseNumber",
                        "MatchQuality": {
                            "City": 0.89,
                            "Street": [
                                1.0
                            ],
                            "HouseNumber": 1.0,
                            "PostalCode": 1.0
                        },
                        "MatchType": "pointAddress",
                        "Location": {
                            "LocationId": "NT_G0ZsvdP1AfF4LQmxtYsKjC_xUD",
                            "LocationType": "point",
                            "DisplayPosition": {
                                "Latitude": 50.11163,
                                "Longitude": 8.66109
                            },
                            "NavigationPosition": [
                                {
                                    "Latitude": 50.11168,
                                    "Longitude": 8.66128
                                }
                            ],
                            "MapView": {
                                "TopLeft": {
                                    "Latitude": 50.1127542,
                                    "Longitude": 8.6593371
                                },
                                "BottomRight": {
                                    "Latitude": 50.1105058,
                                    "Longitude": 8.6628429
                                }
                            },
                            "Address": {
                                "Label": "Savignystraße 15, 60325 Frankfurt am Main, Deutschland",
                                "Country": "DEU",
                                "State": "Hessen",
                                "County": "Frankfurt am Main",
                                "City": "Frankfurt am Main",
                                "District": "Westend",
                                "Street": "Savignystraße",
                                "HouseNumber": "15",
                                "PostalCode": "60325",
                                "AdditionalData": [
                                    {
                                        "value": "Deutschland",
                                        "key": "CountryName"
                                    },
                                    {
                                        "value": "Hessen",
                                        "key": "StateName"
                                    },
                                    {
                                        "value": "Frankfurt am Main",
                                        "key": "CountyName"
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        ]
    }
}

【问题讨论】:

    标签: javascript json react-native


    【解决方案1】:

    我不知道响应的数据是数组还是对象。我认为它是您粘贴为响应的对象。

    应该是这样的

    res.Response.View[0].Result[0].Location.DisplayPosition.Latitude
    res.Response.View[0].Result[0].Location.DisplayPosition.Longitude
    

    【讨论】:

    • 感谢您解决了我的问题。 Didint 知道 View 和 Result 是我必须访问的数组。
    猜你喜欢
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2022-08-18
    • 1970-01-01
    • 2019-05-24
    • 2015-09-07
    • 2017-09-16
    相关资源
    最近更新 更多