Javascript JSON中的地址数?
我有下面的JSON(我验证并验证了它是正确的),我正在尝试获取地址的数量。 当我做一个Javascript JSON中的地址数?,javascript,json,node.js,Javascript,Json,Node.js,我有下面的JSON(我验证并验证了它是正确的),我正在尝试获取地址的数量。 当我做一个 var位置=req.body 我明白了 { AddressValidateRequest: { '-USERID': 'xxxxxxxxxxx', Address: [ [Object], [Object], [Object] ] } } 如何获取地址数 { "AddressValidateRequest": { "-USERID": "xxxxxxxxxxx",
var位置=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"
}
]
}
}
这应该可以
req.body.AddressValidateRequest.Address.length
获取特定地址(例如,第一个地址)
获取地址上的字段
req.body.AddressValidateRequest.Address[0].City
// "New York"
实际上,这不起作用,但由于我将req.body分配给位置,所以我执行了“console.log(location.AddressValidateRequest.Address.length);”它成功了。我尝试了你的建议,但我得到了一个错误:“ReferenceError:request未在Object.handle上定义。请将您的答案更新为location.AddressValidateRequest.Address.length,我会将它标记为已回答,因为您基本上给了我答案。尽管req.body.AddressValidateRequest.Address.length确实有效。对此表示抱歉,您可以随意命名局部变量。我将答案更新为req
。
req.body.AddressValidateRequest.Address[0].City
// "New York"