4

I send a QueryString formatted text like bellow to a php Script via Ajax:

title=hello&custLength=200&custWidth=300  

And I want to convert this text to a JSON Object by this result in PHP:

{
    "title" : "hello",
    "custLength" : 200,
    "custWidth" : 300
}

How can i do that. Does anyone have a solution?

Edit : In fact i have three element in a form by title , custLength and custWidth names and i tried to send these elements via serialize() jquery method as one parameter to PHP script.

this code is for Send data to php:

    customizingOptions  =   $('#title,#custLength,#custWidth').serialize();

$.post('cardOperations',{action:'add','p_id':p_id,'quantity':quantity,'customizingOptions':customizingOptions},function(data){

if (data.success){

    goBackBtn('show');

    updateTopCard('new');           
}                   

},'json');

in PHP script i used json_encode() for convert only customizingOptions parameter to a json.
But the result was not what I expected and result was a simple Text like this:

"title=hello&custLength=200&custWidth=300"
4
  • Which part is it that you're stuck with? Reading the query string or writing the JSON? Commented May 13, 2015 at 12:34
  • echo json_encode($_GET); try this Commented May 13, 2015 at 12:34
  • I have added more details to question. Commented May 13, 2015 at 13:03
  • @ahmad : Added full details to my answer for later reference, simplifying needed code to 2 function calls Commented May 13, 2015 at 15:21

3 Answers 3

12

I realize this is old, but I've found the most concise and effective solution to be the following (assuming you can't just encode the $_GET global):

parse_str('title=hello&custLength=200&custWidth=300', $parsed);

echo json_encode($parsed);

Should work for any PHP version >= 5.2.0 (when json_encode() was introduced).

Sign up to request clarification or add additional context in comments.

Comments

7
$check = "title=hello&custLength=200&custWidth=300";
$keywords = preg_split("/[\s,=,&]+/", $check);
$arr=array();
for($i=0;$i<sizeof($keywords);$i++)
{
$arr[$keywords[$i]] = $keywords[++$i];
}
$obj =(object)$arr;
echo json_encode($obj);

Try This code You Get Your Desired Result

1 Comment

You can use parse_str of php, why you make all that function? php.net/manual/es/function.parse-str.php
3

The easiest way how to achiev JSON object from $_GET string is really simple:

json_encode($_GET)

this will produce the following json output:

{"title":"hello","custLength":"200","custWidth":"300"}

Or you can use some parse function as first (for example - save all variables into array) and then you can send the parsed output into json_encode() function.

Without specifying detailed requirements, there are many solutions.

1 Comment

Thank you for your response. but i want to encode only one parameter of a Get Request that contain a queryString formatted text. I have added more details to question. I thank you once again see

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.