簡體   English   中英

Uncaught SyntaxError:嘗試讀取json數據時出現意外的數字

[英]Uncaught SyntaxError: Unexpected number when trying to read json data

我像這樣從Google Maps api檢索json

$http.get("https://maps.googleapis.com/maps/api/geocode/json?address="+data.SimpleProfileForm.postcode+"&sensor=false")
                                    .success(function(data){
                                        console.log(data);
                                        user.location = data.results.0.address_components.3.long_name;
                                    })
                                    .error(function(data){
                                        console.log("there was an error with the postcode API");
                                        console.log(data);
                                    })

但我在此行上得到了錯誤意外數字:

user.location = data.results.0.address_components.3.long_name;

如果我刪除數字,則會得到“無法讀取未定義的屬性” long_name”。

我如何訪問陣列中的數據? 預先感謝克里斯

您不能使用點符號來索引數組。

您的代碼行可能應顯示為:

user.location = data.results[0].address_components[3].long_name;

當通過JSON進行迭代時,您不能直接引用#鍵。

例如: getJSONitem = parent.221.grandchild.0

為了解決這個問題,您可以通過索引將鍵作為字符串引用,因為JSON密鑰都是字符串。

例如: getJSONitem = parent['221'].grandchild['0']

不必讓他們的字符串,但我認為它是安全的做法的情況下,它們含有隨機字符,如“/ -_”之類的...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM