JSON对象的Typescript模型类

时间:2017-09-30 19:08:50

标签: javascript json angular oop typescript

我有以下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年的条目或仅包含其中一个,等等。

我已经有一个'Property'模型类,我相信它有效,它有所有地址,作者,类别等字段。我正在尝试创建包含这些属性对象列表的outter类:

export interface PropertyWrapper {
  [year: number]: Property;
}

我尝试将JSON解析为PropertyWrapper []数组,这样我就可以通过调用来访问属性:

for (const pw of data) {
        console.log(pw[2017]);
      }

但这只有效,因为我已经知道该对象有一个'2017'条目。无论是否有“2017”,“2010”或10个条目,我怎么能动态地做到这一点?

1 个答案:

答案 0 :(得分:1)

你在找这样的东西吗? 首先获取对象键,然后遍历这些键,然后循环遍历嵌套对象键



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]);
  });
});