代码之家  ›  专栏  ›  技术社区  ›  javapedia.net

在Angular中,如何解析和提取具有复杂结构(JSON)的HTTP响应?

  •  2
  • javapedia.net  · 技术社区  · 6 年前

    我是刚来的,需要你的帮助。我有一个Angular服务,它具有如下所示的API调用函数。

        searcheBay() {
            console.log("calling ebay service");
            return this.httpClient.get (this.ebayURL);
          }
    
    

    我从组件调用这个函数,如下所示。

        this.searchService.searcheBay().subscribe((data) => {
              this.svcdata  =  data
    
          });
    
    

    数据变量具有复杂的JSON结构(见下图)。

    JSON structure

    我要读取的数据由“searchresult”元素保存。您能建议如何解析和提取“searchresult”元素吗?事先谢谢。

    我在SafariDev控制台中进行了调试,并看到了如下所示的元素可访问性。 DEV console debug

    当我在组件中更新相同的代码时,我遇到了编译: src/app/search/search.component.ts(20,29)中出错:错误ts2339:类型“object”上不存在属性“finditeMsbyKeywordsResponse”。 请提出你的想法。

     serviceOnButtonClick(){
     this.searchService.searcheBay().subscribe((data) => {
          this.svcdata  =  data.findItemsByKeywordsResponse[0].searchResult
    
      });
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Ganesh    6 年前

    @javapedia.net如果你回应的话,试试这个。 data 对象与图像中显示的相同,

    this.searchService.searcheBay().subscribe((data) => {
        this.svcdata  =  data.findItemsByKeywordsResponse[0].searchResult;
        console.log(this.svcdata);
    });
    

    编辑

    this.searchService.searcheBay().subscribe((data: any) => {
            this.svcdata  =  data.findItemsByKeywordsResponse[0].searchResult;
            console.log(this.svcdata);
        });
    
        2
  •  0
  •   javapedia.net    6 年前

    最后我使用了如下所示的地图投影。希望这有帮助。

      serviceOnButtonClick(){
     this.searchService.searcheBay().pipe(map((data:any) => data.findItemsByKeywordsResponse[0].searchResult)).subscribe((data) => {
          this.svcdata  =  data;
          console.log(this.svcdata);
      });
    
      }