2015-03-13 74 views
0

獲取數據我有從Ajax POST請求,看起來像這樣的響應:問題與來自JSON對象

{ 
    "columns": [ 
     "r" 
    ], 
    "data": [ 
     [ 
      { 
       "extensions": {}, 
       "start": "http://localhost:7474/db/data/node/2762", 
       "property": "http://localhost:7474/db/data/relationship/2709/properties/{key}", 
       "self": "http://localhost:7474/db/data/relationship/2709", 
       "properties": "http://localhost:7474/db/data/relationship/2709/properties", 
       "type": "IS_CONNECTED_WITH", 
       "end": "http://localhost:7474/db/data/node/2763", 
       "metadata": { 
        "id": 2709, 
        "type": "IS_CONNECTED_WITH" 
       }, 
       "data": { 
        "FOC_Type": "IRU", 
        "DuctType": "IRU", 
        "TrenchType": "IRU", 
        "linestringId": "53805" 
       } 
      } 
     ] 
    ] 
} 

上面是一個字符串。我試圖訪問的是元素:"FOC_Type":"IRU","DuctType":"IRU","TrenchType":"IRU","linestringId":"53805"

我轉換字符串JSON是這樣的:

var response = JSON.parse(response); 

,然後我嘗試訪問某些值是這樣的:

var dataEquip = response[Object.keys(response)[Object.keys(response).length - 1]] // get the data from json obj 

    var komvosName = dataEquip[0][2]; 

但我不能使它發揮作用。

我發現了一種解決方法,我不會將響應轉換爲JSON格式,而是使用字符串。但那不好。 如果有人能告訴我我做錯了什麼,我將不勝感激。

+1

'我將字符串轉換爲JSON',JSON是一個字符串。您正在轉換爲JavaScript對象 – 2015-03-13 14:34:51

+0

Object.keys返回的數組可以按任意順序排列 - 不能保證與您的JSON中的鍵的順序相同。 – radiaph 2015-03-13 14:40:27

回答

1

關於做什麼:

var responseJSON = JSON.parse(response); 
var dataEquip = responseJSON ['data'] // get the data from json obj 
var komvosName = dataEquip['TrenchType']; 

JSON對象(responseJSON)只不過是一個關聯數組更多。

+0

感謝您的回答。使用control.log來檢查komvosName變量會給我[object] [object]。 – user1919 2015-03-13 14:47:13