问题描述
你好,我是angular 2的新手,我从Google,facebook和linkedin获取用户数据.我正在尝试反序列化对象到我创建的名为UserLogin的实例,但是它不起作用.
hello i am new in angular 2 and i am pulling from google , facebook and linkedin the data of user. i am trying to do deserialize object to instance that i created called UserLogin but it doesn't work.
登录方法:
signIn(provider: string) {
this.sub = this._auth.login(provider).subscribe(
(loginDataObj) => {
console.log(loginDataObj);
let jsonConvert: JsonConvert = new JsonConvert();
let user: UserLogin = jsonConvert.deserializeObject(loginDataObj, UserLogin);
console.log(user.email);
//user data
//name, image, uid, provider, uid, email, token (accessToken for Facebook & google, no token for linkedIn), idToken(only for google)
}
)
}
UserLogin类:
UserLogin class:
@JsonObject
export class UserLogins implements IUserLogin{
@JsonProperty("email", String)
public email: String;
@JsonProperty("idToken", String)
public idToken: String;
@JsonProperty("image", String)
public image: String;
@JsonProperty("name", String)
public name: String;
@JsonProperty("provider", String)
public provider: String;
@JsonProperty("token", String)
public token: String;
@JsonProperty("uid", String)
public uid: string;
}
当我将console.log记录到数据时,可以正常打印,但是当我尝试反序列化并打印user.email时,则发送未定义的电子邮件
when i do console.log to the data its printed ok but when i am trying to deserialize and printed the user.email its undefined
感谢帮手.
推荐答案
每个变量都需要初始化.我自己才发现的.@JsonProperty("email",字符串)公开电子邮件:String =";
Each variable needs to be initialized. I just found this out myself. @JsonProperty("email", String) public email: String = "";
这篇关于angular2 json2typescript-jsonConvert.deserializeObject不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!