2017-01-08 32 views
0

我正在使用返回對象的JSON數組的BlockSpring API(從Google表單讀入)。但是,每當我嘗試訪問數組中的對象時,都會返回「未定義」值。我附上了下面的代碼和控制檯日誌。有沒有人有任何想法爲什麼?從對象的JSON數組訪問對象會產生「未定義」

blockspring.runParsed("query-public-google-spreadsheet", { "query": "SELECT A, B, C", "url": 
    "https://docs.google.com/spreadsheets/d/1ZYvcYf_41aghdXRSpg25TKW4Qj9p1Lpz92b1xG-9R1Q/edit?usp=sharing"}, 
    { "api_key": "br_50064_1fe91fe1478ef990dc8b5e9b4041c2c476670306" }, function(res){ 
     var obj=res.params; 
     console.log(obj); 
     var temp=obj[0]; 
     console.log(temp); 
    } 

Console Output

+0

見*「我試圖訪問一個屬性,但我只得到'undefined'回來? 「*在複本的接受答案中。 –

回答

0

您需要使用obj.data[0]訪問數組的第一個元素。

在控制檯中查看您的輸出,看起來您缺少objdata屬性。

對象obj沒有名稱0的屬性,因此它返回undefined

0

我需要自己玩,但我可以告訴問題是你如何訪問信息。

當您嘗試使用var temp=obj[0]獲取數據時,您會像對待數組那樣處理對象。我會建議您嘗試使用此來獲取數據:

//get the actual array 
JSONArray theArray = obj.getJSONArray("data"); //I believe it is stored in an array called data... could be that the obj is just fine 
// now get the first element: 
JSONObject firstItem = theArray.getJSONObject(0); 
// and so on 
String name = firstItem.getString("Name"); // A 

你最有可能可以抓住它使用VAR temp = obj.data[0];

+0

這個問題是關於JavaScript,而不是Java(你的評估仍然是正確的)。 –