我正在使用 nodejs 开发微服务。
请求正在遵循 JSON 。
{
"distCd": "abcd",
"distName": "parentLife Distributor (TOD)",
"stateCd": "",
"subdistInd": false,
"maindistInd": true,
"maindistCd": "",
"distOpendt": "2018-08-28T17:36:02Z",
"pricegrpCd": "01",
"costPricegrpCd": "",
"rssInd": false,
"branchInd": false,
"taxregionCd": "",
"octroiInd": false
}
我如何获得名称“ distCd”和值“ abcd”
答案 0 :(得分:1)
使用.
表示法访问单个属性
var a={
"distCd": "abcd",
"distName": "parentLife Distributor (TOD)",
"stateCd": "",
"subdistInd": false,
"maindistInd": true,
"maindistCd": "",
"distOpendt": "2018-08-28T17:36:02Z",
"pricegrpCd": "01",
"costPricegrpCd": "",
"rssInd": false,
"branchInd": false,
"taxregionCd": "",
"octroiInd": false
};
console.log(a.distCd)
此外,还可以使用[]
var a={
"distCd": "abcd",
"distName": "parentLife Distributor (TOD)",
"stateCd": "",
"subdistInd": false,
"maindistInd": true,
"maindistCd": "",
"distOpendt": "2018-08-28T17:36:02Z",
"pricegrpCd": "01",
"costPricegrpCd": "",
"rssInd": false,
"branchInd": false,
"taxregionCd": "",
"octroiInd": false
};
console.log(a["distCd"])
要获取属性名称,即键,您可以使用Object.keys()。这将为我们提供对象中存在的所有键的数组。
var a={
"distCd": "abcd",
"distName": "parentLife Distributor (TOD)",
"stateCd": "",
"subdistInd": false,
"maindistInd": true,
"maindistCd": "",
"distOpendt": "2018-08-28T17:36:02Z",
"pricegrpCd": "01",
"costPricegrpCd": "",
"rssInd": false,
"branchInd": false,
"taxregionCd": "",
"octroiInd": false
};
console.log(Object.keys(a))
console.log(Object.keys(a)[0]) //to access the first key
要获取对象中的所有值,可以使用Object.values()。这将为我们提供对象中存在的所有值的数组。
var a={
"distCd": "abcd",
"distName": "parentLife Distributor (TOD)",
"stateCd": "",
"subdistInd": false,
"maindistInd": true,
"maindistCd": "",
"distOpendt": "2018-08-28T17:36:02Z",
"pricegrpCd": "01",
"costPricegrpCd": "",
"rssInd": false,
"branchInd": false,
"taxregionCd": "",
"octroiInd": false
};
console.log(Object.values(a))
console.log(Object.values(a)[0]) //to access the first value
答案 1 :(得分:1)
您可以使用Object.keys()和Object.values(),也可以只使用Object.entries()。