如何解析此json响应,以便我可以获取email_address键及其各自的值。它正在返回此功能。我试过response.content.email_address
只返回undefined。它必须非常简单,我似乎无法正确解析它。
由于
function (error, response) {
if ( error ) {
console.log( error );
} else {
console.log( response );
}
Json回复
content: '{"meta":{"pagination":{}},"results":[{"id":"1405904834","status":"ACTIVE","fax":"","addresses":[],"notes":[],"confirmed":false,"lists":[{"id":"1395617465","status":"ACTIVE"}],"source":"Site Owner","email_addresses":[{"id":"d39bf7e0-a2f9-11e7-909a-d4ae528442b5","status":"ACTIVE","confirm_status":"NO_CONFIRMATION_REQUIRED","opt_in_source":"ACTION_BY_OWNER","opt_in_date":"2017-09-26T20:32:33.000Z","email_address":"anders@kitson.org"}],"prefix_name":"","first_name":"Anders","middle_name":"","last_name":"Kitson","job_title":"","company_name":"","home_phone":"","work_phone":"","cell_phone":"","custom_fields":[],"created_date":"2017-09-26T20:32:33.000Z","modified_date":"2017-09-26T20:32:33.000Z","source_details":""},{"id":"1474126978","status":"ACTIVE","fax":"","addresses":[],"notes":[],"confirmed":false,"lists":[{"id":"1395617465","status":"ACTIVE"}],"source":"Site Owner","email_addresses":[{"id":"62874c40-a398-11e7-a559-d4ae5292bb50","status":"ACTIVE","confirm_status":"NO_CONFIRMATION_REQUIRED","opt_in_source":"ACTION_BY_OWNER","opt_in_date":"2017-09-27T15:27:34.000Z","email_address":"test@example.com"}],"prefix_name":"","first_name":"","middle_name":"","last_name":"","job_title":"","company_name":"","home_phone":"","work_phone":"","cell_phone":"","custom_fields":[],"created_date":"2017-09-27T15:27:34.000Z","modified_date":"2017-09-27T15:27:34.000Z","source_details":""}]}'
答案 0 :(得分:0)
您可以使用JSON.parse(response)
然后,您可以将其保存在变量中,以便从那里访问数据。
function(error, response) {
if (error) {
console.log(error);
} else {
var data = JSON.parse(response);
console.log(data.content.email_address) // This should print the value
};
});
答案 1 :(得分:0)
我不得不最终做这样的事情来获得一个电子邮件地址,所以我想我必须运行一个for循环或其他东西才能得到所有这些。
var data = JSON.parse(response.content);
var dataParsed = data.results[0].email_addresses[0].email_address;