【问题标题】:Remove quotes from array value从数组值中删除引号
【发布时间】:2019-06-04 09:56:27
【问题描述】:

我收到了来自 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>

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

【问题讨论】:

  • 不是 JSON 吗?JSON.parse(data)
  • 它是 JSON... JSON 解析在 JSON 中的位置 1 返回 Unexpected token o 它在 php 端编码 echo json_encode($data,true);
  • 能否提供将json字符串返回到angular的代码?
  • 我更新了代码

标签: arrays angular postgresql multidimensional-array


【解决方案1】:

你可以添加

$gifts = json_decode($gifts)

到您的 PHP 代码。见here。

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

JSON.parse(data)

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

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 2021-11-29
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多