其中一个JSON值返回未识别的JavaScript

时间:2013-05-23 13:05:36

标签: javascript json

我需要使用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
                            }
                        ]
                    }
                ]
            }]

代码适用于所有字段,除非我访问Identities,否则返回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始终返回unidentified。也行不通:

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

1 个答案:

答案 0 :(得分:4)

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

你可能想要

var id = ip[0].identities;

或者你应该迭代fields.contacts[i]['identity-profiles'],但目前还不清楚你究竟想要什么。