raj_tx
raj_tx

Reputation: 239

How to read a particular value from the json.stringify: ionic

From the post i have got a res as

.subscribe( res => {
    let result = JSON.stringify(res);
    alert(result)
})

& as the response of alert(result) I’m getting

{"access_token": "hjhdshJHUHYI67HUjhhk98BJ_BJHBBHbnbhbhbjh_jmnhghmghgfffgxcgfcf98hujh",
 "expires_in":"518399"}

Here I want to read the access_token value & save it in a variable how Can I do it?

Upvotes: 1

Views: 676

Answers (4)

Raj
Raj

Reputation: 638

.subscribe( res => {
     alert(res.access_token)
 })

If you are unable to solve your problem by following solution..

There is no need to stringify the result. Please try following..

.subscribe( res => {
    alert(res['access_token']);
})

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222552

You dont have to stringify, just access it as res.access_token

.subscribe((res:any) => {
  let result = res.access_token;
  alert(result)
})

Upvotes: 2

Ayoub k
Ayoub k

Reputation: 8868

.subscribe( res => {
    alert(res.access_token)
})

Upvotes: 0

Joe Iddon
Joe Iddon

Reputation: 20414

Just access that property of the object with the usual syntax.

.subscribe( res => {
    let the_variable = res['access_token']; //or `result.access_token`
})

Upvotes: 1

Related Questions