Javascript 如何解析嵌套的JSON对象数组?

Javascript 如何解析嵌套的JSON对象数组?,javascript,arrays,node.js,json,typescript,Javascript,Arrays,Node.js,Json,Typescript,我们正在为WeClapp()编写扩展。WeClapp中的自定义属性可以添加到几乎所有的数据类型中。这些属性可以通过嵌套的JSON对象数组访问 接口示例: export interface ICustomAttribute { attributeDefinitionId: string; booleanValue?: boolean; dateValue?: number; entityId?: string; numberValue?: number;

我们正在为WeClapp()编写扩展。WeClapp中的自定义属性可以添加到几乎所有的数据类型中。这些属性可以通过嵌套的JSON对象数组访问

接口示例:

export interface ICustomAttribute {
    attributeDefinitionId: string;
    booleanValue?: boolean;
    dateValue?: number;
    entityId?: string;
    numberValue?: number;
    selectedValueId?: string;
    stringValue?: string;
}

export interface IContact {
    id: string;
    ...
    customAttributes: ICustomAttribute[];
    email: string;
    ...
} 
响应示例:

{
  "id": "4317",
  ...
  "customAttributes": [
    {
      "attributeDefinitionId": "4576",
      "booleanValue": null,
      "dateValue": null,
      "entityId": null,
      "numberValue": null,
      "selectedValueId": null,
      "selectedValues": null,
      "stringValue": "Test"
    },
    {
      "attributeDefinitionId": "69324",
      "booleanValue": true,
      "dateValue": null,
      "entityId": null,
      "numberValue": null,
      "selectedValueId": null,
      "selectedValues": null,
      "stringValue": null
    }
  ],
  ...
  "email": "name@domain.com",
  ...
}
除了
[customAttributes]
属性外,访问IContact(以下示例中为contact)属性没有问题。如何访问和操作数据

在以下示例中,
contact=IContact

console.log(contact);
[{"id":"102871",...,"customAttributes":[{"attributeDefinitionId":"4229"},{"attributeDefinitionId":"46381"},{"attributeDefinitionId":"69316"},{"attributeDefinitionId":"98781","stringValue":"77b5d0f1-b1a4-4957-8ea2-ea95969e3c03"}],...,"email":"name@domain.com"}]
产出:

[
  {
    id: '102570',
    ...
    customAttributes: [ [Object], [Object], [Object], [Object] ],
    ...
  }
]
JavaScript数组扩展(
length
ForEach
,…)在
[contact.customAttributes]
上不可用,因为它是
未定义的

let attributes = new Array(0);
attributes = attributes.concat(record.customAttributes);

console.log(attributes); // Outputs: [undefined]
我还尝试更改接口并尝试重新分析:

export interface IContact {
    id: string;
    ...
    customAttributes: string;
    email: string;
    ...
} 

...

let attributes Array<ICustomAttribute>  = JSON.Parse(record.customAttributes);
此新条目可以按上图所示进行过账和返回

JSON.stringify(联系人)
的输出:

console.log(contact[“customAttributes”])
的输出是
未定义的

尝试
console.log(contact[“customAttributes”])

如果您的
控制台.log(联系人)
生成:

[
  {
    id: '102570',
    ...
    customAttributes: [ [Object], [Object], [Object], [Object] ],
    ...
  }
]

这表明
contact.customAttributes
不存在
contact
是一个对象数组,要访问第一个对象,您需要
contact[0]。customAttributes

如何从API获取数据?你可能有一个未兑现的承诺吗?我正在运行一个restify服务器。一个端点将数据预加载到持久内存阵列(阵列)中的模块中。其他端点在该数组(.forEach)中循环。我不会在项目的任何部分使用承诺。只有npm包是restify和request(不是承诺的包)。请尝试使用JSON.stringify记录IContact,以便您可以准确地看到您得到的内容。@matt helliwell添加了:JSON.stringify(contact)的输出:result=>undefinedthx,以获取回复。我可以访问其他属性(请参阅:console.log(contact.id);//Outputs:102570)。因此,联系人对象不是集合。我错了吗?
[{"id":"102871",...,"customAttributes":[{"attributeDefinitionId":"4229"},{"attributeDefinitionId":"46381"},{"attributeDefinitionId":"69316"},{"attributeDefinitionId":"98781","stringValue":"77b5d0f1-b1a4-4957-8ea2-ea95969e3c03"}],...,"email":"name@domain.com"}]
[
  {
    id: '102570',
    ...
    customAttributes: [ [Object], [Object], [Object], [Object] ],
    ...
  }
]