2015-12-30 32 views
1

我有數據結果:發生次數字的JavaScript

{ 
    "statusCode": "T", 
    "statusMessage": "Success", 
    "resultFeasibility": { 
    "resultStatus": 7005, 
    "resultInfo": "Fiber 
    access is available" 
    }, 
    "devices": { 
    "deviceID": "2573631", 
    "hostName": "ODP-SKB-FCP\/003 FCP\/D01\/003.01", 
    "networkLocation": "ODP-SKB-FCP\/003", 
    "technology": "GPON", 
    "stoCode": "SKB", 
    "maxSpeed": null, 
    "double": 0, 
    "icon": "resources\/images\/icon-gphone-blue.png", 
    "address": { 
     "latitude": "-6.915", 
     "longitude": "106.9185", 
     "zipCode": null, 
     "district": null, 
     "city": null, 
     "streetName": null, 
     "lineOne": null 
    }, 
    "markerId": "7-2573631", 
    "type": "ALPRO" 
    } 
} 

我想顯示「設備ID」發生了多少,這是我的代碼: 代碼:

if (result.statusCode == 'T') { 
    this.odp = result.devices; 
    console.log(this.odp); 
    if (this.odp.length > 0) { 
    var alproType = this.odp.technology; 
    if (alproType == 'DSLAM') 
     this.feasibilityResult.push('feasibility DSLAM: ' + this.odp.length + ' DSLA'); 
    else if (alproType == 'MSAN') 
     this.feasibilityResult.push('feasibility MSAN: ' + this.odp.length + ' MSAN'); 
    else 
     this.feasibilityResult.push('feasibility ODP: ' + this.odp.length + ' ODP'); 
    } 

但輸出一片空白。任何人都可以幫助我?由於

+0

看起來像'this.odp.length> 0'是'FALSE'。 –

+0

您可以更新多個設備的JSON – Rajesh

+0

哪個值完全爲null?您的第一個console.log日誌記錄爲空嗎?或者它是可行性結果數組? – Glubus

回答

1

會發生什麼事是,設備不是一個數組,這就是爲什麼沒有長度,則必須將JSON更改爲:

{"statusCode":"T","statusMessage":"Success","resultFeasibility":{"resultStatus":7005,"resultInfo":"Fiber 
access is available"},"devices":[{"deviceID":"2573631","hostName":"ODP-SKB-FCP\/003 FCP\/D01\/003.01" 
,"networkLocation":"ODP-SKB-FCP\/003","technology":"GPON","stoCode":"SKB","maxSpeed":null,"double":0 
,"icon":"resources\/images\/icon-gphone-blue.png","address":{"latitude":"-6.915","longitude":"106.9185" 
,"zipCode":null,"district":null,"city":null,"streetName":null,"lineOne":null},"markerId":"7-2573631" 
,"type":"ALPRO"}]} 

我的意思是把裏面brakets []的設備,然後你可以調用屬性長度。 否則就必須改變的條件下,例如

if(this.odp != undefined){...} 

希望這有助於

+0

響應來自另一個程序,我剛剛從中獲得了數據。我的意思是,我無法改變回應。無論如何要做到這一點?謝謝,我會添加條件。 –