[英]Typescript model class for JSON object
我有以下JSON:
{
"-KtDGS8GdOcJ33Lcqjok": {
"2017": {
"address": "test address 1",
"area2": "3212",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Solar",
"client": "Test Contact",
"createdAt": 1504551753483,
"lastEdited": 1504551805648,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.490362758827665",
"lng": "-69.93279173970221",
"name": "17002 - test",
"pictures": {
"a95ff256-0f05-3122-a6b5-a88d3fd14c3f": true
},
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "1234",
"year": "2017"
}
},
"-KtDGaU9BB6eNj-MsyBg": {
"2015": {
"address": "test 2a",
"area1": "3245",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Local: comercio",
"client": "test2 ",
"createdAt": 1504552100747,
"lastEdited": 1504552100747,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.489417118875462",
"level": "4",
"lng": "-69.92930956184864",
"name": "15002 - test2a ",
"parking": "12",
"plaza": "Agora",
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"restrooms": "2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "213",
"year": "2015"
},
"2017": {
"address": "test 2",
"area1": "3245",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Local: comercio",
"client": "test2 ",
"createdAt": 1504551790632,
"lastEdited": 1504551790632,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.489417118875462",
"level": "4",
"lng": "-69.92930956184864",
"name": "17003 - test2b",
"parking": "12",
"plaza": "Agora",
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"restrooms": "2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "213",
"year": "2017"
}
},
"codeCounter": {
"2015": 2,
"2017": 5
},
"totals": {
"2015": 1,
"2017": 5
}
}
它基本上是一個對象列表,每個對象包含一個或多個嵌套的對象,這些對象在制作年份包裝(實際上是房屋,公寓等)。
我遇到麻煩的地方是嘗試映射“年”對象(2017、2015等),因為它們可能存在或可能不存在。 例如,一個對象可能同時具有2017年,2016年條目或其中之一,依此類推。
我已經有一個相信可以使用的“屬性”模型類,它具有所有地址,作者,類別等字段。 我正在嘗試創建將包含這些屬性對象列表的外部類:
export interface PropertyWrapper {
[year: number]: Property;
}
我嘗試將JSON解析為PropertyWrapper []數組,通過這種方式,我已經可以通過調用以下方式訪問屬性:
for (const pw of data) {
console.log(pw[2017]);
}
但這只能起作用,因為我已經知道對象具有“ 2017”條目。 無論是否有“ 2017”,“ 2010”或10個條目,我如何動態地執行此操作?
您在尋找這樣的東西嗎? 首先獲取對象鍵,然后遍歷這些鍵,然后遍歷嵌套的對象鍵
var jso = { "-KtDGS8GdOcJ33Lcqjok": { "2017": { "address": "test address 1", "area2": "3212", "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92", "category": "Solar", "client": "Test Contact", "createdAt": 1504551753483, "lastEdited": 1504551805648, "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92", "lat": "18.490362758827665", "lng": "-69.93279173970221", "name": "17002 - test", "pictures": { "a95ff256-0f05-3122-a6b5-a88d3fd14c3f": true }, "price": "213", "province": "-KtDBavhyLhrpV8hDuj2", "sector": "-KtDBqgy3CqpTv6c_iQ9", "totalPrice": "1234", "year": "2017" } }, "-KtDGaU9BB6eNj-MsyBg": { "2015": { "address": "test 2a", "area1": "3245", "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92", "category": "Local: comercio", "client": "test2 ", "createdAt": 1504552100747, "lastEdited": 1504552100747, "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92", "lat": "18.489417118875462", "level": "4", "lng": "-69.92930956184864", "name": "15002 - test2a ", "parking": "12", "plaza": "Agora", "price": "213", "province": "-KtDBavhyLhrpV8hDuj2", "restrooms": "2", "sector": "-KtDBqgy3CqpTv6c_iQ9", "totalPrice": "213", "year": "2015" }, "2017": { "address": "test 2", "area1": "3245", "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92", "category": "Local: comercio", "client": "test2 ", "createdAt": 1504551790632, "lastEdited": 1504551790632, "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92", "lat": "18.489417118875462", "level": "4", "lng": "-69.92930956184864", "name": "17003 - test2b", "parking": "12", "plaza": "Agora", "price": "213", "province": "-KtDBavhyLhrpV8hDuj2", "restrooms": "2", "sector": "-KtDBqgy3CqpTv6c_iQ9", "totalPrice": "213", "year": "2017" } }, "codeCounter": { "2015": 2, "2017": 5 }, "totals": { "2015": 1, "2017": 5 } }; Object.keys(jso).forEach(function(key) { Object.keys(jso[key]).forEach(function(nestedKey){ console.log(`nestedKey: ${nestedKey} \\n ` , jso[key][nestedKey]); }); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.