2014-07-09 18 views
-1

我有以下的JSON(我驗證和驗證是正確的),我試圖得到地址的數量。 當我做了JSON中的地址數量?

var location = req.body;

我得到

{ AddressValidateRequest: 
    { '-USERID': 'xxxxxxxxxxx', 
    Address: [ [Object], [Object], [Object] ] } } 

我如何獲得的地址數量?

{ 
    "AddressValidateRequest": { 
     "-USERID": "xxxxxxxxxxx", 
     "Address": [ 
      { 
       "-ID": "0", 
       "FirmName": "firmname", 
       "Address1": "address1here", 
       "Address2": "13 infinite loop", 
       "City": "new york", 
       "State": "NY", 
       "Zip5": "zip5here", 
       "Zip4": "zip4here" 
      }, 
      { 
       "-ID": "1", 
       "FirmName": "firmhere", 
       "Address1": "address1here", 
       "Address2": "1 Smith Ct ", 
       "City": "San Predo", 
       "State": "CA", 
       "Zip5": "ziphere", 
       "Zip4": "ziphere1" 
      }, 
      { 
       "-ID": "1", 
       "FirmName": "firmhere", 
       "Address1": "address1here", 
       "Address2": "12 John Rd ", 
       "City": "Newark", 
       "State": "PA", 
       "Zip5": "ziphere", 
       "Zip4": "ziphere1" 
      } 
     ] 
    } 
} 

回答

1

這應該這樣做

req.body.AddressValidateRequest.Address.length 

爲了得到一個特定的地址(例如,第一)

req.body.AddressValidateRequest.Address[0] 

要獲得一個地址字段

req.body.AddressValidateRequest.Address[0].City 
// "New York" 
+0

實際上,這並沒有奏效,但是因爲我將req.body分配給位置I did'console.log(location.AddressValidateRequest.Address.length);'它的工作。我嘗試了你的建議,我得到一個錯誤:「ReferenceError:請求未定義
   在Object.handle」 – ConfusedDeer

+0

請更新您的答案location.AddressValidateRequest.Address.length,我會標記它的答案,因爲你基本上給我答案。 – ConfusedDeer

+0

儘管req.body.AddressValidateRequest.Address.length確實有效。 – ConfusedDeer