1

我需要使用 JavaScript 从 JSON 中检索电子邮件字段。这是代码:

        "contacts": [
            {
                "addedAt": 1332358711001,
                "vid": 1,
                "properties": {
                    "lastname": {
                        "value": "Mott"
                    },
                    "firstname": {
                        "value": "Adrian"
                    }
                },

                "identity-profiles": [
                    {
                        "vid": 1,
                        "identities": [
                            {
                                "type": "EMAIL",
                                "value": "test-fdfc6c2e-e19e-4138-8201-8342ca333aa1@hubspot.com",
                                "timestamp": 1332358711715
                            },
                            {
                                "type": "LEAD_GUID",
                                "value": "f3ebaf07-1c6d-4ada-af31-3559dd8b3027",
                                "timestamp": 1332358711771
                            }
                        ]
                    }
                ]
            }]

该代码适用于所有字段,除了当我到达身份时,它返回 NULL 或未识别。

var temp = fields.contacts.length;
for (var i=0; i<fields.contacts.length; i++){

  var addedAt = fields.contacts[i].addedAt;
  var formattedDate = Utilities.formatDate(new Date(addedAt), "GMT", "yyyy-MM-dd");

  var lastName = fields.contacts[i].properties.lastname.value;
  var firstName = fields.contacts[i].properties.firstname.value;
  var vid = fields.contacts[i].vid;
  var ip = fields.contacts[i]['identity-profiles'];

  var id = ip.identities;
}

var id 总是返回未识别的。也不起作用:

for (var j=0; i<id.length; j++){
  if(typeof ['type'] == 'EMAIL'){
    var email = id[j].value;
  }
};
4

1 回答 1

4

fields.contacts[i]['identity-profiles']是一个数组,它没有直接的identities属性。

你可能想要

var id = ip[0].identities;

或者你应该迭代fields.contacts[i]['identity-profiles']但不清楚你到底想要什么。

于 2013-05-23T13:08:55.103 回答