ionic 3-将嵌套JSON中的数据提取到数组中

时间:2018-07-28 14:47:13

标签: json angular ionic-framework ionic3

所以我在尝试解析json文件以将其存储到数组时遇到问题。

这是我从以下位置解析JSON的API网站:https://data.gov.sg/api/action/datastore_search?resource_id=139a3035-e624-4f56-b63f-89ae28d4ae4c&limit=5

下载文件成功,但是现在的问题是,每次我尝试通过console.log()查看内容时,都将其内容解析为数组,但我不断得到[object Object]的回报。我的目标是将json中的结果/记录的内容存储到一个数组中,并将其返回给父方法

我在某处做错了吗?

这是我的代码:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class CarparkInfoService {

  constructor(public http: HttpClient) {
    console.log('Carpark Info Service Provider');
  }
  getCarparkData(): Observable<any> {
    let data1: Observable<any>;
    data1 = this.http.get('https://data.gov.sg/api/action/datastore_search?resource_id=139a3035-e624-4f56-b63f-89ae28d4ae4c&limit=5');
    console.log("data=" + data1); // Returns [object Object]
    return data1;
  }
}

1 个答案:

答案 0 :(得分:0)

因此,问题在于您希望查看json数据,而甚至没有从服务中提取json数据,因为控制台日志是可以观察到的:

//以下返回可观察值

<ul class="thumbnails">
<?php
  if ( !empty($products_small_image)) {
    echo '<li><a class="thumbnail" href="'.  OSC::link($template->getDirectoryTemplateImages() . $products_image_zoom) .'" title="' . $products_name . '"><img src="' . OSC::link($template->getDirectoryTemplateImages() . $products_image_medium) . '" title="' . $products_name . '" alt="' . $products_name . '"></a></li>';

-----> take all images forte gallery
    $Qpi = $Db->get('products_images', ['image', 'htmlcontent'], ['products_id' => $id], 'sort_order');
    $pi = $Qpi->fetchAll();

    $pi_total = count($pi);

      if ($pi_total > 0) {
?>
    <div>
<?php
        $pi_counter = 0;
        $pi_html = [];

        foreach ($pi as $image) {
          $pi_counter++;

          if (!empty($image['htmlcontent'])) {
            if ($this->getVideo($image['htmlcontent']) === true) {

              $pi_html[] = '<div id="button_' . $pi_counter . '">' . $image['htmlcontent'] . '</div>';

              $video = $image['htmlcontent'];
              $footer_tag .= '<script src="' . $template->getTemplateDefaultJavaScript('magnific/ClicShopping/youtube.js.php') . '"></script>' . "\n";
              $template->addBlock($footer_tag, 'footer_scripts');

===> pb here
              echo '<button class="button_' . $pi_counter . '" style="padding:20px;">Open popup</button>';
            }
          }

          echo '<li class="image-additional"><a class="thumbnail" href="' . CLICSHOPPING::link($template->getDirectoryTemplateImages() . $image['image']) . '" title="' . $products_name . ' ">';
          echo '<img src="' . CLICSHOPPING::link($template->getDirectoryTemplateImages() . $image['image']) . '" width="70" height="70" title="' . $products_name . '" alt="' . $products_name . ' id="piGalImg_' . $pi_counter . '"></a>';
          echo '</li>';
        }

        if ( !empty($pi_html) ) {
          echo '    <div style="display: none;">' . implode('', $pi_html) . '</div>';
        }
      } else {
?>

    <div class="pull-right">
        <?php echo HTML::image(OSC::link($products_small_image), $products_name); ?>
      </div>

<?php
      }
    }
?>
</ul>

因此,要获取和解析数据,您需要订阅此可观察对象,该对象将一次调用此api:

getCarparkData(): Observable<any> {
    let data1: Observable<any>;
    data1 = this.http.get('https://data.gov.sg/api/action/datastore_search?resource_id=139a3035-e624-4f56-b63f-89ae28d4ae4c&limit=5');
    console.log("data=" + data1); // Returns [object Object]
    return data1;
}

因此,请确保您订阅了可观察的对象,它将“激活”对API的调用