[英]How i can read the nested properties and pushing to the array using javascript?
我想閱讀 JSON 屬性。 如果類型是數組或 object 或 jsonp,那么我必須讀取嵌套屬性並需要將它推入一個也應該嵌套的數組。 喜歡:
{
name:"test",
type:"array"
[name]: {
name: "test1",
type: "..."
}
}
JSON object 閱讀:
{
"properties": {
"value": {
"type": "array",
"items": {
"required": [
"@odata.etag",
"id",
"createdDateTime",
"lastModifiedDateTime",
"changeKey",
"originalStartTimeZone",
"originalEndTimeZone",
"iCalUId",
"reminderMinutesBeforeStart",
"isReminderOn",
"hasAttachments",
"subject",
"bodyPreview",
"importance",
"sensitivity",
"isAllDay",
"isCancelled",
"isOrganizer",
"responseRequested",
"showAs",
"type",
"webLink",
"isOnlineMeeting",
"onlineMeetingProvider",
"allowNewTimeProposals",
"isDraft",
"hideAttendees"
],
"properties": {
"id": {
"type": "string",
"minLength": 1
},
"end": {
"type": "object",
"required": [
"dateTime",
"timeZone"
],
"properties": {
"dateTime": {
"type": "string",
"minLength": 1
},
"timeZone": {
"type": "string",
"minLength": 1
}
}
},
"body": {
"type": "object",
"required": [
"contentType",
"content"
],
"properties": {
"content": {
"type": "string",
"minLength": 1
},
"contentType": {
"type": "string",
"minLength": 1
}
}
},
"type": {
"type": "string",
"minLength": 1
},
"start": {
"type": "object",
"required": [
"dateTime",
"timeZone"
],
"properties": {
"dateTime": {
"type": "string",
"minLength": 1
},
"timeZone": {
"type": "string",
"minLength": 1
}
}
},
"showAs": {
"type": "string",
"minLength": 1
},
"iCalUId": {
"type": "string",
"minLength": 1
},
"isDraft": {
"type": "boolean"
},
"subject": {
"type": "string",
"minLength": 1
},
"webLink": {
"type": "string",
"minLength": 1
},
"isAllDay": {
"type": "boolean"
},
"location": {
"type": "object",
"required": [
"displayName",
"locationType",
"uniqueIdType",
"address",
"coordinates"
],
"properties": {
"address": {
"type": "object",
"required": [],
"properties": {}
},
"coordinates": {
"type": "object",
"required": [],
"properties": {}
},
"displayName": {
"type": "string"
},
"locationType": {
"type": "string",
"minLength": 1
},
"uniqueIdType": {
"type": "string",
"minLength": 1
}
}
},
"attendees": {
"type": "array",
"items": {
"required": [
"type"
],
"properties": {
"type": {
"type": "string",
"minLength": 1
},
"status": {
"type": "object",
"required": [
"response",
"time"
],
"properties": {
"time": {
"type": "string",
"minLength": 1
},
"response": {
"type": "string",
"minLength": 1
}
}
},
"emailAddress": {
"type": "object",
"required": [
"name",
"address"
],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"address": {
"type": "string",
"minLength": 1
}
}
}
}
},
"minItems": 1,
"uniqueItems": true
},
"changeKey": {
"type": "string",
"minLength": 1
},
"locations": {
"type": "array",
"items": {
"required": [],
"properties": {}
}
},
"organizer": {
"type": "object",
"required": [
"emailAddress"
],
"properties": {
"emailAddress": {
"type": "object",
"required": [
"name",
"address"
],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"address": {
"type": "string",
"minLength": 1
}
}
}
}
},
"categories": {
"type": "array",
"items": {
"required": [],
"properties": {}
}
},
"importance": {
"type": "string",
"minLength": 1
},
"recurrence": {},
"@odata.etag": {
"type": "string",
"minLength": 1
},
"bodyPreview": {
"type": "string",
"minLength": 1
},
"isCancelled": {
"type": "boolean"
},
"isOrganizer": {
"type": "boolean"
},
"sensitivity": {
"type": "string",
"minLength": 1
},
"isReminderOn": {
"type": "boolean"
},
"hideAttendees": {
"type": "boolean"
},
"onlineMeeting": {},
"transactionId": {},
"hasAttachments": {
"type": "boolean"
},
"responseStatus": {
"type": "object",
"required": [
"response",
"time"
],
"properties": {
"time": {
"type": "string",
"minLength": 1
},
"response": {
"type": "string",
"minLength": 1
}
}
},
"seriesMasterId": {},
"createdDateTime": {
"type": "string",
"minLength": 1
},
"isOnlineMeeting": {
"type": "boolean"
},
"onlineMeetingUrl": {},
"responseRequested": {
"type": "boolean"
},
"originalEndTimeZone": {
"type": "string",
"minLength": 1
},
"lastModifiedDateTime": {
"type": "string",
"minLength": 1
},
"allowNewTimeProposals": {
"type": "boolean"
},
"onlineMeetingProvider": {
"type": "string",
"minLength": 1
},
"originalStartTimeZone": {
"type": "string",
"minLength": 1
},
"reminderMinutesBeforeStart": {
"type": "number"
}
}
},
"minItems": 1,
"uniqueItems": true
},
"@odata.context": {
"type": "string",
"minLength": 1
},
"@odata.nextLink": {
"type": "string",
"minLength": 1
}
}
}
此代碼將返回所有嵌套值
propertiesObj = "";// the above json
contextProperties = [];
generate_properties(propertiesObj, propertiesArray) {
Object.entries(propertiesObj).forEach(([key, value]) => {
// console.log(value);
let contextObj = {};
contextObj["attrName"] = key;
contextObj["attrId"] = key;
contextObj["datatype"] = value["type"];
contextObj["attrType"] = "output";
if (value["type"] == "array" || value["type"] == "object") {
contextObj[key] = [];
if (Object.keys(value).includes("items")) {
this.generate_properties(
value["items"]["properties"],
contextObj[key]
);
} else {
this.generate_properties(value["properties"], contextObj[key]);
}
propertiesArray.push(contextObj);
} else {
propertiesArray.push(contextObj);
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.