Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取数组json而不是对象json_Json_Angular_Api_Service_Format - Fatal编程技术网

如何获取数组json而不是对象json

如何获取数组json而不是对象json,json,angular,api,service,format,Json,Angular,Api,Service,Format,我做了一些自动完成功能,它需要一些数组方法。我想使用数组json作为API,而不是对象json 因为filter方法只对数组有效。我必须使用数组json[]作为API url,但我的API是一个对象json{}文件。如何使其成为一个数组? 我尝试了一些数组json API。该代码可用于数组json,但不能用于对象json HTML: 我希望API JSON是数组或自动完成功能。在您的服务中尝试使用:from而不是of。看看这个 这是一个基于您的代码的工作 <mat-aut

我做了一些自动完成功能,它需要一些数组方法。我想使用数组json作为API,而不是对象json

因为filter方法只对数组有效。我必须使用数组json[]作为API url,但我的API是一个对象json{}文件。如何使其成为一个数组? 我尝试了一些数组json API。该代码可用于数组json,但不能用于对象json

HTML:


我希望API JSON是数组或自动完成功能。

在您的服务中尝试使用:
from
而不是
of
。看看这个

这是一个基于您的代码的工作

         <mat-autocomplete #auto="matAutocomplete" [displayWith]="getOptionText">
          <mat-option *ngFor="let option of (filteredOptions | async)" [value]="option">
            {{ option.make }}
          </mat-option>
         </mat-autocomplete>
@Injectable({
  providedIn: 'root'
})
export class Service {
  constructor(private http: HttpClient) { }

  opts = [];

  getData() {
    return this.opts.length ?
      of(this.opts) :
      this.http.get<any>('https://api.myjson.com/bins/15psn9').pipe(map(data => this.opts = data));
  }
}
  constructor(private service: Service) { }

  ngOnInit() {
    this.filteredOptions = this.searchForm.controls['customerId'].valueChanges.pipe(
      startWith(''),
      debounceTime(100),
      switchMap(value => value.length >= 3 ? this.doFilter(value) : [])
    );
  }

  doFilter(value) {
    return this.service.getData()
      .pipe(
        map(response => response.filter((option: { make: { toLowerCase: () => { indexOf: (arg0: any) => number; }; }; }) => {
          return option.make.toLowerCase().indexOf(value.toLowerCase()) === 0;
        }))
      );
  }

  getOptionText(option) {
    return option.make;
  }