Angular http请求如何将Json Object转换为对象Array

时间:2018-03-09 01:26:14

标签: json angular http get

我想通过读取Json文件及其存储的数据来模拟http请求。这是我尝试的方式

Data.json:

{ "jsonData": {


"data1": [{
   "age": "18",
   "name": "bernd",
   "gender": "1"
}],

"data2": [{ "age": "18", "name": "bernd", "gender": "1"},
    { "age": "18", "name": "bernd", "gender": "1"},
    { "age": "15", "name": "bernd", "gender": "1"},
    { "age": "18", "name": "marting", "gender": "1"},
    { "age": "25", "name": "bernd", "gender": "1"},
    { "age": "29", "name": "bernd", "gender": "1"},
    { "age": "18", "name": "mike", "gender": "1"},
    { "age": "45", "name": "bernd", "gender": "1"},
    { "age": "18", "name": "bernd", "gender": "1"}
],

"data3": [{ "age": "18", "name": "mike", "gender": "1"},
    { "age": "18", "name": "monika", "gender": "2"},
    { "age": "18", "name": "martin", "gender": "1"},
    { "age": "18", "name": "monika", "gender": "2"},
    { "age": "18", "name": "monika", "gender": "2"},
    { "age": "18", "name": "monika", "gender": "2"},
    { "age": "18", "name": "bernd", "gender": "1"},
    { "age": "18", "name": "bernd", "gender": "1"},
    { "age": "18", "name": "lisa", "gender": "2"}
]}}

以及收到数据的服务

data.service:

getServers() {
return this.http.get('../assets/Data.json')
.subscribe((res: Response) => {
  this.testData = res.json().data2;
} );}

计划是将“data2”对象数组转换为“this.testData”。我尝试了不同版本的http.get,但只是设法获取Json对象而没有成功将其转换为对象[]。

我真的很感谢你的帮助。

0 个答案:

没有答案