I have an url passing parameters use json_encode each values like follow:
$json = array ( 'countryId' => $_GET['CountryId'], 'productId' => $_GET['ProductId'], 'status' => $_GET['ProductId'], 'opId' => $_GET['OpId'] ); echo json_encode($json);
It's returned a result as:
{ "countryId":"84", "productId":"1", "status":"0", "opId":"134" }
Can I use json_decode
to parse each values for further data processing?
Thanks.
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).
The json_encode() function is used to encode a value to JSON format.
You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.
json_decode()
will return an object or array if second value it's true:
$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}'; $json = json_decode($json, true); echo $json['countryId']; echo $json['productId']; echo $json['status']; echo $json['opId'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With