New to angular, i'm trying to do a simple thing! I'd like to convert a json to a specific object. Still doesn't work at this time.
The JSON is acquired from an url parameter "token" encoded in base64.
Here is the code:
this.decodedToken: ViewToken = JSON.parse(JSON.stringify(this.DecodeBase64(this.token)));
console.log('token exists: ' + this.token);
console.log('token decoded: ' + this.decodedToken);
console.log('token target: ' + this.decodedToken.target);
console.log('token access: ' + this.decodedToken.accessToken);
The target Object:
export class ViewToken {
target: string;
accessToken: string;
}
And here is the result:
token exists: ew0KICAgIHRhcmdldDogIm15VGFyZ2V0IiwNCiAgICBhY2Nlc3NUb2tlbjogIlRHOXlaVzBnYVhCemRXMGdaRzlzYjNJZ2MybDBJR0Z0WlhRc0lHTnZibk5sWTNSbGRIVnlJR0ZrYVhCcGMyTnBibWNnWld4cGRBPT0iDQp9
token decoded: {
target: "myTarget",
accessToken: "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdA=="
}
token target: undefined
token access: undefined
I tried many solutions but none of them worked! I still can't map the JSON to my object.
Solutions I tried so far:
- json2typescript package -> gives me error on the deserialize function
fromJson method -> gives me a table of each character in the json
static fromJson(json: any) { let obj: IViewToken = Object.create(ViewToken.prototype); return Object.assign(obj, json, { target: json.target, accessToken: json.accessToken }); }
object mapping key,value to object -> doesn't work since the json object attributes are undefined
EDIT:
Correction to make it work: I don't have to JSON.stringify my decoded base64 token
this.decodedToken = JSON.parse(this.DecodeBase64(this.token));