如何解析嵌套的JSON对象数组? [英] How to parse nested JSON object arrays?
问题描述
我们正在为WeClapp( https://www.weclapp.com/api2/).WeClapp中的自定义属性可以添加到几乎所有数据类型中.这些属性可通过嵌套的JSON对象数组访问.
We're writing an Extension for WeClapp (https://www.weclapp.com/api2/). Custom Attributes in WeClapp could be added to almost all Datatypes. These Attributes are accessible over a nested JSON object Array.
接口示例:
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",
...
}
访问IContact(在下面的示例中为联系人)属性没有问题,期望使用 [customAttributes]
属性.如何访问和处理数据?
There is no Problem accessing the IContact (contact in the following example) properties expect the [customAttributes]
property. How can I access and manipulate Data?
在以下示例中, contact = IContact
:
console.log(contact);
输出:
[
{
id: '102570',
...
customAttributes: [ [Object], [Object], [Object], [Object] ],
...
}
]
console.log(contact.id); //Outputs: 102570
console.log(contact.customAttributes); //Outputs: undefined
JavaScript数组扩展( length
, ForEach
,...)在 [contact.customAttributes]
上不可用,因为它是未定义
:
JavaScript Array Extensions (length
, ForEach
, ...) are not available on [contact.customAttributes]
, because it's undefined
:
let attributes = new Array(0);
attributes = attributes.concat(record.customAttributes);
console.log(attributes); // Outputs: [undefined]
我也尝试更改界面并尝试重新解析:
I also tried to Change the interface and tried to reparse:
export interface IContact {
id: string;
...
customAttributes: string;
email: string;
...
}
...
let attributes Array<ICustomAttribute> = JSON.Parse(record.customAttributes);
我不知道为什么无法访问该阵列.最奇怪的是设置属性不会引发任何错误:
I have no idea why I can't Access the Array. The strangest thing is that setting attributes does not throw any errors:
let attribute: ICustomAttribute = { attributeDefinitionId: "4576", stringValue: "Test" };
let contact = { customAttributes: [attribute] };
此新条目可以过帐并返回,如上所示.
This new entry could be posted and Returns as shown above.
JSON.stringify(contact)
的输出:
[{"id":"102871",...,"customAttributes":[{"attributeDefinitionId":"4229"},{"attributeDefinitionId":"46381"},{"attributeDefinitionId":"69316"},{"attributeDefinitionId":"98781","stringValue":"77b5d0f1-b1a4-4957-8ea2-ea95969e3c03"}],...,"email":"name@domain.com"}]
console.log(contact ["customAttributes"])的输出是 undefined
.
Output of console.log(contact["customAttributes"])
is undefined
.
推荐答案
如果您的 console.log(联系人)
产生:
[
{
id: '102570',
...
customAttributes: [ [Object], [Object], [Object], [Object] ],
...
}
]
这表明 contact.customAttributes
不存在. contact
是一个对象数组,要到达第一个对象,您需要 contact [0] .customAttributes
Then this suggests that contact.customAttributes
does not exist. contact
is an array of objects, and to reach the first, you would need contact[0].customAttributes
这篇关于如何解析嵌套的JSON对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!