简体   繁体   English

在Angular2中使用Typescript读取JSON

[英]Reading JSON using Typescript in Angular2

After doing a POST to Firebase, I have this returned from Firebase 在对Firebase进行POST之后,我从Firebase返回了此信息

{ "name": "-KBfn7iFNIxSuCL9B-g5" } 
  1. How do I use Typescript to read the response returned from the POST request? 如何使用Typescript读取POST请求返回的响应? Only the value -KBfn7iFNIxSuCL9B-g5 is need for routing. 路由仅需要值-KBfn7iFNIxSuCL9B-g5 My existing incomplete code looks like this: 我现有的不完整代码如下所示:
   addHeroGotoHeroDetail() {
     let heroId: string;
     this._firebaseService.postHero()
        .subscribe(response => {
          //do something to make the heroId = -KBfn7iFNIxSuCL9B-g5 
     });
     this._router.navigate(['hero-detail', {id: heroId}]);
   }

The goal is to have a button in the homepage where user can click on it to create a new hero Id in Firebase then redirect it to the newly created hero detail page, where he can do more detail updating. 目标是在主页上有一个按钮,用户可以单击该按钮在Firebase中创建新的英雄ID,然后将其重定向到新创建的英雄详细信息页面,在该页面中,他可以进行更多详细信息更新。


Also after doing a GET from Firebase, I have this returned from Firebase 此外,从Firebase执行GET之后,我从Firebase返回了此信息

{ "-KBfn-cw7wpfxfGqbAs8": { "created": 1456728705596, "hero": "Hero 1", "...": "..."} }
  1. Should I create an interface for the hero JSON returned by Firebase? 我应该为Firebase返回的英雄JSON创建一个接口吗?

  2. If yes for question 2, how should the interface look? 如果问题2是,则界面应如何显示?

export interface X {
  id: string; //id defined by Firebase e.g. -KBfn-cw7wpfxfGqbAs8 
  hero: string; //name of the hero e.g. Hero 1 
  created: number; // The time when the hero was created
}
  1. If yes for question 2, how do I use Typescript to parse the JSON object to the interface? 如果问题2是,则如何使用Typescript将JSON对象解析到接口? Code sample would be very much appreciated. 代码示例将不胜感激。

The goal is to display the hero detail to the user, and allow the user to add more details to the hero such as Date of Birth, Gender and etc... and then update Firebase with these changes. 目标是向用户显示英雄详细信息,并允许用户向英雄添加更多详细信息,例如出生日期,性别等,然后使用这些更改更新Firebase。

1. See below. 1.参见下文。

2. Yes, you should create an interface that matches the return type from Firebase. 2.是的,您应该创建一个与Firebase的返回类型匹配的接口。

3. Based on your example JSON, this is how I would structure the return type's interface: 3.根据您的示例JSON,这就是构造返回类型的接口的方式:

export interface FirebaseResponse {
    [id: string]: {
        created: number;
        hero: string;
    }
}

4. To parse the response from Firebase into a strongly-typed TypeScript object, do something like this: 4.要将Firebase的响应解析为强类型的TypeScript对象,请执行以下操作:

let firebaseResponse = <FirebaseResponse>JSON.parse(response);

You can then return only the ID, if you wish, by doing something like this: 然后,您可以按照以下方式仅返回ID:

// note: this is making some assumptions about your response type.
// You may need a more sophisticated way of returning the ID depending
// on the structure of the response - for example, if the object has
// more than one key.
return Object.keys(firebaseResponse)[0];

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

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