简体   繁体   中英

in php ! how to extract a variable value of a response (json) and echo it

i get a response of my code . that response has a variable value which is before a constant string and after another constant string . i want get this variable and echo it .

example : i have this json:

{ "id": "tok_1Cs0osBsNOaFjmrSIBFEAkPd", "object": "token", "client": 
{ "id": "card_1Cs0osBsNOaFjmrSl9fAs5hT", "object": "name", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": "74455", "address_zip_check": "unchecked", "country": "RU",  "metadata": {}, "name": "2022 Nemelek\r\n", "tokenization_method": null }, "client_ip": "41.199.157.183", "created": 1532578278, "livemode": true, "type": "Uid", "used": false }

i wanna extract tok_1Cs0osBsNOaFjmrSIBFEAkPd which is after { "id": " and before ", "object":

btw : this value is variable but what is before and first word after it are constants

how can i code it in php

Use json_decode() :

$str = '{ "id": "tok_1Cs0osBsNOaFjmrSIBFEAkPd", "object": "token", "client": { "id": "card_1Cs0osBsNOaFjmrSl9fAs5hT", "object": "name", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": "74455", "address_zip_check": "unchecked", "country": "RU", "metadata": {}, "name": "2022 Nemelek\r\n", "tokenization_method": null }, "client_ip": "41.199.157.183", "created": 1532578278, "livemode": true, "type": "Uid", "used": false }';

$v = json_decode($str);

//tok_1Cs0osBsNOaFjmrSIBFEAkPd
echo $v->id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM