从数组值中删除引号

时间:2019-01-09 15:21:26

标签: arrays angular postgresql multidimensional-array

我收到了来自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 return =从数据库中删除引号,但不能使用。

$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>

不能。获取图像,因为它是作为字符串返回的...需要删除引号以使其起作用

1 个答案:

答案 0 :(得分:0)

您可以添加

$gifts = json_decode($gifts)

至您的PHP代码。参见here

或者,正如xyz建议的那样,在javascript端进行解析,例如

JSON.parse(data)

毕竟,这与引号无关,但必须最后由Javascript代码将其理解为JSON。