将 JSON 值返回到对象 IONIC v2



我有一个 API,它基于 Ionic V2 中的条形码扫描函数返回一个值。 请参阅下面的代码片段:

getProduct(qrBarcode: string): Observable<Product[]> {
  return this.http.get(`${this.ApiUrl}/product/read_product.php?barcode=${qrBarcode}`)
    .map(res => <Product[]>res.json());
}

我使用一个函数将搜索从条形码扫描仪集成到 API 阅读如下:

scanCode(){
this.barcodeScanner.scan().then(barcodeData => {
this.scannedCode = barcodeData.text;
this.qrBarcode = this.scannedCode;
this.apiProvider.getProduct(this.qrBarcode).subscribe(products => {
  this.products = products;
  this.dosingRequirement = products[1].Dosage;
})
//test api calculations
this.totalVolume = this.tankVolume;
this.dosage = this.dosingRequirement * this.totalVolume;
}

面临的问题是我无法分解 JSON,因此返回并将 JSON 的每个部分作为对象返回。在这方面的任何帮助都将是非常棒的。

并没有真正得到你从问题输出中得到的东西。但是似乎您应该尝试从.map(res => <Product[]>res.json())中删除<Product[]>,因此就像.map(res => res.json())一样,因为map作用于列表的每个元素,并且将列表的每个元素类型转换为(Product[])内的另一个列表是没有意义的。如果这不能解决它,可以尝试创建问题的重现,或者提供有关输出中得到的内容与输出中期望的内容的更多信息。

希望对你有帮助

最新更新