How to read key and value from a json string?

Asked

Viewed 634 times

-1

I looked at some scripts and found nothing like it...
Could I just get the part:

EAAAAAYsX7TsBAsdfosfbsJZBrbPOzrIXkkdoCgqg5oT15RC3ZC4QZCAVg8WLXugwhuYsuUi0E92XBEuTRge0HZC298cwYjNVPbnBZAIpOL9kshfisdisM3MuCg8kALZB0sKjunVWTSMmTYq1dUfK8eQ81i8aNcLmzsfhtdFON6XvmRA5oI8MCVZAOKPD3ROVQbwfwQZA54zGe6zrah6nw5Lz07VrAZD

With the command explodes, or with another?

Below is the complete code as I am generating:
I left it in bold for whoever is going to give me a little help, give a check in an easier way...

{"session_key":"5.4Kt_bhcGLE530g.1500797842.40-122201203876562","uid":122201203876562,"secret":"a89s8we0e1f7767c747a0ca69084df","access_token":"**EAAAAAYsX7TsBAsdfosfbsJZBrbPOzrIXkkdoCgqg5oT15RC3ZC4QZCAVg8WLXugwhuYsuUi0E92XBEuTRge0HZC298cwYjNVPbnBZAIpOL9kshfisdisM3MuCg8kALZB0sKjunVWTSMmTYq1dUfK8eQ81i8aNcLmzsfhtdFON6XvmRA5oI8MCVZAOKPD3ROVQbwfwQZA54zGe6zrah6nw5Lz07VrAZD**","machine_id":"klt0WVBPc1BWzTSlzlsI0W8T","session_cookies":[{"name":"c_user","value":"122201203876562","expires":"Mon, 23 Jul 2018 08:17:22 GMT","expires_timestamp":1532333842,"domain":".facebook.com","path":"\/","secure":true},{"name":"xs","value":"40:4Kt_bhcGLE530g:2:1500797842:10534:11960","expires":"Mon, 23 Jul 2018 08:17:22 GMT","expires_timestamp":1532333842,"domain":".facebook.com","path":"\/","secure":true,"httponly":true},{"name":"fr","value":"8dsdsfsfUvar14Q.AWXQFSahahfsgsk88_-kEag.BWzwiw.0y.Flt.0.0.BZdFuS.AWV8aN6a","expires":"Mon, 23 Jul 2018 08:17:22 GMT","expires_timestamp":1532333842,"domain":".facebook.com","path":"\/","secure":true,"httponly":true},{"name":"datr","value":"klt0WVBPc1BWzTSlzlsI0W8T","expires":"Tue, 23 Jul 2019 08:17:22 GMT","expires_timestamp":1563869842,"domain":".facebook.com","path":"\/","secure":true,"httponly":true}],"confirmed":true,"identifier":"email_sjn\u0040hotmail.com"}

Could someone please let me know how I can get this?

Thank you.

  • You should have more than 100 questions like this: https://answall.com/search?q=php+ler+json

3 answers

1

Wouldn’t it be better to use json_decode, once it is in json format.

Do so:

$json_string ='{"session_key":"5.4Kt_bhcGLE530g.1500797842.40-122201203876562","uid":122201203876562,"secret":"a89s8we0e1f7767c747a0ca69084df","access_token":"EAAAAAYsX7TsBAsdfosfbsJZBrbPOzrIXkkdoCgqg5oT15RC3ZC4QZCAVg8WLXugwhuYsuUi0E92XBEuTRge0HZC298cwYjNVPbnBZAIpOL9kshfisdisM3MuCg8kALZB0sKjunVWTSMmTYq1dUfK8eQ81i8aNcLmzsfhtdFON6XvmRA5oI8MCVZAOKPD3ROVQbwfwQZA54zGe6zrah6nw5Lz07VrAZD","machine_id":"klt0WVBPc1BWzTSlzlsI0W8T","session_cookies":[{"name":"c_user","value":"122201203876562","expires":"Mon, 23 Jul 2018 08:17:22 GMT","expires_timestamp":1532333842,"domain":".facebook.com","path":"/","secure":true},{"name":"xs","value":"40:4Kt_bhcGLE530g:2:1500797842:10534:11960","expires":"Mon, 23 Jul 2018 08:17:22 GMT","expires_timestamp":1532333842,"domain":".facebook.com","path":"/","secure":true,"httponly":true},{"name":"fr","value":"8dsdsfsfUvar14Q.AWXQFSahahfsgsk88_-kEag.BWzwiw.0y.Flt.0.0.BZdFuS.AWV8aN6a","expires":"Mon, 23 Jul 2018 08:17:22 GMT","expires_timestamp":1532333842,"domain":".facebook.com","path":"/","secure":true,"httponly":true},{"name":"datr","value":"klt0WVBPc1BWzTSlzlsI0W8T","expires":"Tue, 23 Jul 2019 08:17:22 GMT","expires_timestamp":1563869842,"domain":".facebook.com","path":"/","secure":true,"httponly":true}],"confirmed":true,"identifier":"email_sjn\u0040hotmail.com"}';

$value = json_decode($json_string);

var_dump($value->access_token);

Teste de código

The variable $value now contains the json values in object form.

Just do $value->chave to get the value. In your case the value you want is access_token therefore replace chave for acess_token.

0

@Taissonmartins, looking at the tags of your question I’m imagining that you can also use Javascript, so although you already got the answer you needed, here’s another option. I separated your JSON just to make it easier to read...

var myObj = {
    "session_key":"5.4Kt_bhcGLE530g.1500797842.40-122201203876562",
    "uid":122201203876562,
    "secret":"a89s8we0e1f7767c747a0ca69084df",
    "access_token":"**EAAAAAYsX7TsBAsdfosfbsJZBrbPOzrIXkkdoCgqg5oT15RC3ZC4QZCAVg8WLXugwhuYsuUi0E92XBEuTRge0HZC298cwYjNVPbnBZAIpOL9kshfisdisM3MuCg8kALZB0sKjunVWTSMmTYq1dUfK8eQ81i8aNcLmzsfhtdFON6XvmRA5oI8MCVZAOKPD3ROVQbwfwQZA54zGe6zrah6nw5Lz07VrAZD**",
    "machine_id":"klt0WVBPc1BWzTSlzlsI0W8T",
    "session_cookies":[
                                {
                                    "name":"c_user",
                                    "value":"122201203876562",
                                    "expires":"Mon, 23 Jul 2018 08:17:22 GMT",
                                    "expires_timestamp":1532333842,
                                    "domain":".facebook.com",
                                    "path":"\/",
                                    "secure":true
                                },
                                {
                                    "name":"xs",
                                    "value":"40:4Kt_bhcGLE530g:2:1500797842:10534:11960",
                                    "expires":"Mon, 23 Jul 2018 08:17:22 GMT",
                                    "expires_timestamp":1532333842,
                                    "domain":".facebook.com",
                                    "path":"\/",
                                    "secure":true,
                                    "httponly":true
                                },
                                {
                                    "name":"fr",
                                    "value":"8dsdsfsfUvar14Q.AWXQFSahahfsgsk88_-kEag.BWzwiw.0y.Flt.0.0.BZdFuS.AWV8aN6a",
                                    "expires":"Mon, 23 Jul 2018 08:17:22 GMT",
                                    "expires_timestamp":1532333842,
                                    "domain":".facebook.com",
                                    "path":"\/",
                                    "secure":true,
                                    "httponly":true
                                },
                                {
                                    "name":"datr",
                                    "value":"klt0WVBPc1BWzTSlzlsI0W8T",
                                    "expires":"Tue, 23 Jul 2019 08:17:22 GMT",
                                    "expires_timestamp":1563869842,
                                    "domain":".facebook.com",
                                    "path":"\/",
                                    "secure":true,
                                    "httponly":true
                                }
                            ],
    "confirmed":true,
    "identifier":"email_sjn\u0040hotmail.com"
}

console.log(myObj.access_token);

This will print:

**EAAAAAYsX7TsBAsdfosfbsJZBrbPOzrIXkkdoCgqg5oT15RC3ZC4QZCAVg8WLXugwhuYsuUi0E92XBEuTRge0HZC298cwYjNVPbnBZAIpOL9kshfisdisM3MuCg8kALZB0sKjunVWTSMmTYq1dUfK8eQ81i8aNcLmzsfhtdFON6XvmRA5oI8MCVZAOKPD3ROVQbwfwQZA54zGe6zrah6nw5Lz07VrAZD**
  • Thanks @wBB clarified me further;

0

More like to know if there is any way to do this via the explode command? For only the part of the token;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.