I am returning JSON from restful API
getUsers() {
return this.http.get('https://myAPI.net/api/TAFGetAllUsers/0')
}
which is called in
users: object;
ngOnInit() {
this.data.getUsers().subscribe(data => {
this.users = data
console.log(this.users)
})
}
However, the console output is plain JSON text:
{ "Users": [
{
"UserId": "4B4D1C12-48FD-4C1D-91F7-03C18FEC8B86",
"UserTypeId": 1,
"Name": "Tommy",
"EmailAddress": "email1@pie.co.uk",
"DateCreated": "2019-03-22T09:28:04.553"
},
{
"UserId": "232D7596-3DD8-40A1-B4B0-15A54A6A102A",
"UserTypeId": 3,
"Name": "Alexis",
"CompanyName": "Sony",
"EmailAddress": "ds3@sony.com",
"DateCreated": "2019-03-20T11:53:53.360"
},
{
"UserId": "1BB22695-1B4D-4E42-8A95-16D1E9B1EF59",
"UserTypeId": 3,
"Name": "Richard",
"CompanyName": "Microsoft",
"EmailAddress": "email2@bob.com",
"DateCreated": "2019-03-20T13:57:22.183"
} ] }
It isn't seen as an object. If I do
data[0]
it just returns '['
Also if I do
data.Users
it returns "undefined".
I'm unable to directly do
JSON.parse(data)
as it sees data as an object, giving an error of "Argument of type 'Object' is not assignable to parameter of type 'string'"
I've tried converting the data object to string then parse to JSON
this.rawUsers = JSON.stringify(data);
this.users = JSON.parse(this.rawUsers);
But this outputs the same as the default data value, acting like a string.