Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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 Angular2,从REST调用获取数据_Json_Rest_Angular - Fatal编程技术网

Json Angular2,从REST调用获取数据

Json Angular2,从REST调用获取数据,json,rest,angular,Json,Rest,Angular,我要通过id从json文件中获取数据,通过获取所有内容。 这里是JSON: [ { "id": "1", "name": "Carlos", "apellidos":"López", "edad":"30", "ciudad":"Hospitalet" }, { "id": "2", "name": "Arantxa", "apellidos":"Pavia", "edad":"24", "ciudad":"Barcelona" }, { "id": "3", "name": "Didac" ,

我要通过id从json文件中获取数据,通过获取所有内容。 这里是JSON:

[
{ "id": "1", "name": "Carlos", "apellidos":"López", "edad":"30", "ciudad":"Hospitalet" },
{ "id": "2", "name": "Arantxa", "apellidos":"Pavia", "edad":"24", "ciudad":"Barcelona" },
{ "id": "3", "name": "Didac" , "apellidos":"Pedra", "edad":"muchos", "ciudad":"Cornellà" },
{ "id": "4", "name": "Daniel" , "apellidos":"Farnos", "edad":"nolose", "ciudad":"Barcelona" }
]
服务:

private usersUrl = 'app/users.json'; 
getUser(id: String): Observable<User>{
    let body = JSON.stringify(
      {
        "token": "test",
        "content": {
            "id": id
          }
        }
   );
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({
        headers: headers,
        body : body
      });

    return this.http.get(this.usersUrl, options)
        .map(res => res.json()).catch(this.handleError);
}
Console.log:

Array[4]0: Object1: Object2: Object3: Objectlength: 4__proto__: Array[0]
JSON坏吗


谢谢。

因为您没有按id筛选结果

.map(res => res.json())
.map(x > x.find(x => x.id == id) // filter by selected id
.catch(this.handleError);
.map(res => res.json())
.map(x > x.find(x => x.id == id) // filter by selected id
.catch(this.handleError);