angular2에서 JSON 파일을 구문 분석하려고하지만 어떻게 작동하는지 알 수 없습니다.angular2의 json 내부에서 json을 구문 분석하는 방법
user.service.ts
getInfo(): Observable<any>{
let url= 'example.json';
return this.http.get(url, options)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'retriving data failed'));
}
user.component.ts
template: `<p>{{userInfoes.user_info.user_id}}</p>`
export class USER{
getInfo(): void{
this.userService.getInfo().subscribe(userInfoes =>this.userInfoes= userInfoes);
}}
example.json
{
"task": "angular2",
"user_info": {"user_id": "yeehaah", "user_location": "canada"}
}
그래서 예를 들어 JSON 파일입니다. 재산은 또 다른 json입니다. json 파일을 구문 분석하기 위해 map을 사용할 때, 단지 한 번 구문 분석하고 user_info 속성에는 문자열 (json)을 남겨 둡니다. 구문 분석 된 example.json은 다른 json을 남겨 두었다는 것을 설명합니다.
내가 어떻게해야 example.json
{
task: "angular2",
user_info: {"user_id": "yeehaah", "user_location": "canada"}
}
나는 example.json 파일의 user_info
건물 내부 user_id
을 표시하려면 구문 분석?
"다른 json"이란 무엇을 의미합니까? 이들은 * javascript 객체 *이므로 전체 페이로드의 JSON을 구문 분석하면 추가 파싱없이 액세스 할 수 있습니다. 예 :'var id = user_info.user_id' 또는'var id = user_info [ 'user_id]' –
'console.log (user_info.user_id)'를 사용 했으므로 정의되지 않았 음을 알립니다. –
JSON을 파싱하는 마법은 없습니다. 'JSON.parse'를 사용하면 파싱 된 JSON을 리턴합니다. –