如何格式化来自api响应Angular HttpClient的数据

时间:2018-03-16 09:58:03

标签: json angular httpclient

我正在尝试制作一个有角度的HttpClient get请求,但是在处理数据方面遇到了麻烦。

我正在使用以

形式返回数据的api
(10)[{...}, {...}, {...}, ...]
[
0:{
   row: Array(4)
     0: "someid"
     1: "somename"
     2: "someaddress"
     3: "somepostcode"
  },
1:{
   row: Array(4)
     0: "someid"
     1: "somename"
     2: "someaddress"
     3: "somepostcode"
  },
2:{
   row: Array(4)
     0: "someid"
     1: "somename"
     2: "someaddress"
     3: "somepostcode"
  }, 
...
]

我只是希望能够使用get数据中的“someaddress”和“someid”字段创建对象列表。我已经查看了各种教程,但仍然获得未定义的元素。你怎么能从这里获得数据。

2 个答案:

答案 0 :(得分:4)

你可以这样做:

> dput(sample)
structure(list(chrom = c("chr10", "chr20"), start = c(121780088L, 
54214412L), end = c(121780259L, 54215291L), score = c(0.27, 0.78
), genehancer_id = c("GH10G121780", "GH20G054214"), connected_gene = c("TACC2", 
"PFDN4"), score.1 = c("3.94", "0.43"), connected_gene.1 = c("ATE1", 
"DOK5"), score.2 = c("1.38", "0.06"), connected_gene.2 = c("GC10M121752", 
NA), score.3 = c("0.31", NA), connected_gene.3 = c("GC10M121821", 
NA), score.4 = c("0.22", NA)), .Names = c("chrom", "start", "end", 
"score", "genehancer_id", "connected_gene", "score.1", "connected_gene.1", 
"score.2", "connected_gene.2", "score.3", "connected_gene.3", 
"score.4"), class = "data.frame", row.names = c(NA, -2L))

希望它有所帮助! :)

答案 1 :(得分:0)

如果要键入HTTP响应,请使用泛型类型:

interface MyInterface {
  row: string[];
}

return this.http.get<MyInterface[]>('URL');