How can I convert php's POST variable to json format?
You convert the whole array to JSON as one object by calling JSON. stringify() on the array, which results in a single JSON string. To convert back to an array from JSON, you'd call JSON. parse() on the string, leaving you with the original array.
The JSON. stringify() method converts a JavaScript object, array, or value to a JSON string. If you so choose, you can then send that JSON string to a backend server using the Fetch API or another communication library.
To convert PHP array to JSON, use the json_encode() function. The json_encode() is a built-in PHP function that converts an array to json. The json_encode() function returns the string containing a JSON equivalent of the value passed to it, as demonstrated by the numerically indexed array.
If this is entirely in php, and you simply want to convert the data in $_POST
to JSON, you can do so like this:
$json = json_encode($_POST);
Using php's built-in function json_encode
(doc)
If this is not what you want to do, please be more specific with your question.
json_encode($_POST);
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