简体   繁体   English

Angular-将HAL转换为JSON

[英]Angular - convert HAL to JSON

The following service extracts category objects from a REST service which returns them in HAL format. 以下服务从REST服务中提取类别对象,该服务以HAL格式返回它们。 Now I try to convert that response into JSON. 现在,我尝试将响应转换为JSON。 For that I searched and tried different solutions, eg chariotsolutions or so . 为此,我搜索并尝试了不同的解决方案,例如chariotsolutions 左右 Some are based on Response from '@angular/http' which is deprecated and which I cannot make work. 有些基于“ @ angular / http”的响应,因此已弃用,并且我无法进行工作。

How can I do the conversion? 我如何进行转换?

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs/Rx';
import { of } from 'rxjs/observable/of';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';

import { Category } from './category';

@Injectable()
export class CategoryService {

  private categoriesUrl = 'http://localhost:8080/account/categories';

  constructor(private http: HttpClient) { }

  getCategories(): Observable<Category[]> {
    return this.http.get<Category[]>(this.categoriesUrl);
  }

}

The response as HAL 回应为HAL

{
  "_embedded": {
    "categories": [
      {
        "id": 1,
        "name": "hardware",
        "description": "comprises all computer hardware",
        "level": "FIRST",
        "_links": {
          "self": {
            "href": "http://localhost:8080/account/categories/1"
          },
          "categoryEntity": {
            "href": "http://localhost:8080/account/categories/1"
          }
        }
      },
      {
        "id": 2,
        "name": "hardware_notebook",
        "description": "all notebooks",
        "level": "SECOND",
        "_links": {
          "self": {
            "href": "http://localhost:8080/account/categories/2"
          },
          "categoryEntity": {
            "href": "http://localhost:8080/account/categories/2"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/account/categories{?page,size,sort}",
      "templated": true
    },
    "profile": {
      "href": "http://localhost:8080/account/profile/categories"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 8,
    "totalPages": 1,
    "number": 0
  }
}
getCategories(): Observable<Category[]> {
    return this.http.get<Category[]>(this.categoriesUrl)
        .map((result:any)=>{
           console.log(result); //<--it's an object
           //result={"_embedded": {"categories": [..]..}
           return result._embedded.categories; //just return "categories"
        });
}

With Rjxs 6.0 we must use pipe(map) 在Rjxs 6.0中,我们必须使用pipe(map)

getCategories(): Observable<Category[]> {
    return this.http.get<Category[]>(this.categoriesUrl).pipe(
        map((result:any)=>{
           console.log(result); //<--it's an object
           //result={"_embedded": {"categories": [..]..}
           return result._embedded.categories; //just return "categories"
        }));
}

Try following: 请尝试以下操作:

getCategories(): Observable<Category[]> {
    return this.http.get<Category[]>(this.categoriesUrl).map((response)=>{
        return response;
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM