convert POST array to json format - php

How can I convert php's POST variable to json format?

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);
Docs: http://php.net/manual/en/function.json-encode.php

Related

Get Json Value with extra quotes using php

Get all value from json using json_decode. Given name is Juliet"es this json. If using json_decode this array will be change null value. how do i get this json to array
$jsonobj = '{"Name":"Juliet"es","Maths":37,"English":43}';
if anyone having any idea please post answer.
Try $convertedJson = json_decode(addslashes($jsonobj));.
But has suggested by #Barmar and #CBroe , always generated JSON strings from array or object with json_encode().

JSON_ENDODE why use to pass a PHP array?

I want to convert an array of objects to JSON encoding, but how it is possible to encode PHP array into JSON array.
if you want to convert php array into json
use:
json_encode($array);
json_encode
for decode
json_decode($array);
json_decode

Help editing JSON to make an array rather than a 'dictionary'

I currently have json using json_encode from a mysql query which looks like this:
{"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}, {"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}
How can I have the json being an array of posts ('post_2', 'post_1') rather than it being a dictionary? The JSON will be decoded on an iPhone using SBJSON and the JSON will have to be made into an array in the backend.
Thanks in advanced.
Provide a non-associative array to json_encode(). The easiest way is usually to simply call array_values() on the (associative) array, and encode the result.
Take a look at PHP's json_decode function, specifically the 2nd parameter if you want an array.

how to convert json to string

how do i convert a json object to a string
i want to insert a json object to a mysql DB
You might be interested in json_encode().
On the other hand if you already got something json encoded then it already is a string and you can simply store it as-is in the database.
JSON itself is a string.
People use json_encode() and json_decode() to convert objects/arrays of PHP to string and back to objects/arrays.
$array = json_decode($json,true);
$string = implode(",",$array);
Now you can save this string in db as text.

Best way to pass JSON from Browser to PHP using Ajax.Request

Hi I have a JSON object that is a 2-dimentional array and I need to pass it to PHP using Ajax.Request (only way I know how). ...Right now I manually serialized my array using a js function...and get the data in this format: s[]=1&d[]=3&[]=4 etc. ....
my question is: Is there a way to pass the JSON object more directly/efficientely?..instead of serializing it myself?
Thanks for any suggestions,
Andrew
You can also use Prototype's function toJSON() to convert an array into a JSON object. After passing it to server via Ajax call, simply use PHP's function json_decode() to decode the object.
Pass the object as a JSON-string to PHP, and in PHP use the builtin json_decode to get a PHP-object from the string.
In Javascript, use a "stringify" function on your object to get it as a string, library available for example here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
In que Javascript side (with Prototye):
var myJSON= Object.toJSON(youArray);
In que Php side:
$myjson = $_POST['myjson'];
$arrayJSON= json_decode(stripslashes($myjson), true);
Check
http://www.openjs.com/scripts/data/ued_url_encoded_data/
to encode nested data directly correct, since Object.toQueryString() doesn't accept nested data...

Categories