这是我从 GET 请求得到的响应
我需要从中提取ID的值
“DE:SM - 请求问题的附加/备用联系人”:“{“objCode”:“USER”,“name”:“Priyanka Haridas”,“ID”:“5ede31210141630d03517f6ec308a3c0”}“
我尝试了多种方法,但似乎都不起作用。
此脚本可以提取ID。
const object = JSON.parse(jsonData.data[0].parameterValues['DE:SM - Primary contact for request questions']);
console.log(object.ID)
#1“JSON 中的键名包含空格”
"parameterValues": {
"DE:SM - Primary contact for request questions": value
}
该格式可以访问一个值
parameterValues['带空格的键名']
parameterValues['DE:SM - Primary contact for request questions']
let values = {
"DE:SM - Primary contact for request questions": "{\"objCode\":\"USER\",\"name\":\"Priyanka Haridas\",\"ID\":\"5ede31210141630d03517f6ec308a3c0\"}"
};
console.log(values['DE:SM - Primary contact for request questions']);
结果
#2 值包括 \"
您需要通过
JSON.parse()
将对象转换为 JSON 对象
"{\"objCode\":\"USER\",\"name\":\"Priyanka Haridas\",\"ID\":\"5ede31210141630d03517f6ec308a3c0\"}"
JSON.parse(值)
let values = {
"DE:SM - Primary contact for request questions": "{\"objCode\":\"USER\",\"name\":\"Priyanka Haridas\",\"ID\":\"5ede31210141630d03517f6ec308a3c0\"}"
};
const data1 = values['DE:SM - Primary contact for request questions']
console.log("data1.ID: " + data1.ID);
const data2 = JSON.parse(values['DE:SM - Primary contact for request questions'])
console.log("data2.ID: " + data2.ID);
结果
Postman 的完整演示 在
Tests
选项卡
const jsonData = JSON.parse(responseBody);
console.log(jsonData.data[0].parameterValues['DE:SM - Primary contact for request questions']);
const object = JSON.parse(jsonData.data[0].parameterValues['DE:SM - Primary contact for request questions']);
console.log(object.ID)