首页 > 解决方案 > 从数组值中删除引号

问题描述

我有一个来自 postgres DB 的响应,其中包括数组周围的 qoutes

images: "[{"image":"https://dw901jjvwj0lk.cloudfront.net/smart-address/image/gift certificate/afa9b56f-ddb4-4ae5-b72b-afae20129ea4/gift certificate/IMG_5076.JPG"}]"
info: "[{"balance_owo":0,"balance_btc":0,"btc_address":0,"btc_qr":null,"price_usd":25,"details":"use this gift certificate at sweet sosumba on Georgia and Park NW","quantity":500,"quantity_left":null}]"

引号阻止 *ngFor 意识到这是一个数组

我尝试使用 jsonb 返回 = 从数据库中删除引号,但不起作用。

$query=$dbh->prepare("SELECT *, earth_distance(ll_to_earth($user_lat,$user_lng), ll_to_earth(lat, lng)) as local_distance
FROM tribe.smart_address
WHERE earth_box(ll_to_earth($user_lat,$user_lng), $fence) @> ll_to_earth(lat, lng) AND active=:one
ORDER by date_created DESC");
$query->bindValue(':one', 1);
$query->execute();
$gifts = $query -> fetchAll()

我也试过

[src]="removeQuotes(data.images)"
[src]="data.images[0].image"

没有任何作用...消毒也不起作用...我该如何解决,请

这是从数据库中获取数据的角度代码

    this.$local.gift_start(this.user_token, this.user_id, 
  this.user_lat, this.user_lng, this.type)
  .then((jordi: any) => {
    if (jordi.success) {
      this.Dsmart = jordi.payload[0]
   }
  )

从服务返回承诺

    return new Promise(resolve => {
  this.http.post('https://owo.world/app/gift/start', DATA, httpOptions)
    .subscribe(data => {
      resolve(data);
    }, err => {
      console.log(err);
    });
});

首页

<div class="card current_progress" *ngFor="let data of 
   Dsmart; let i = index">
          <a [href]="data[i].images"> <img class="img-fluid img-raised" 
  [src]="data[i].images[0].image" alt=""> </a>

它不能。获取图像,因为它作为字符串返回...需要删除引号才能使其工作

标签: arraysangularpostgresqlmultidimensional-array

解决方案


你可以添加

$gifts = json_decode($gifts)

到您的 PHP 代码。见这里

或者,正如 xyz 建议的那样,在 javascript 端解析它,比如

JSON.parse(data)

毕竟,这与引号无关,但它最终必须被您的 Javascript 代码理解为 JSON。


推荐阅读