vitto
vitto

Reputation: 19466

PHP: parse JSON from jQuery

I'm trying to parse a simple JSON string, but I'm not used to do it from PHP. To do my test I've written a page where the data is created; sends request to PHP page to do some tests.

How can I access the JSON data from PHP and have it returned back?

The string from jQuery:

var json_str = '{"id_list":[{"id":"2","checked":"true"},{"id":"1","checked":"false"}]}';
/*'{
    "id_list": [
        {
            "id": "2",
            "checked": "true"
        },{
            "id": "1",
            "checked": "false"
        }
    ]
}'*/
$.post ("php_json_parser.php", { data_to_send:json_str }, function (data_back) {
    alert (data_back);
});

PHP page:

<?php

$data_back = json_decode (stripslashes($_REQUEST['data_to_send']), true);
print $data_back->{'id_list'[0]["id"]}; //??? how can I access to properties?

?>

Upvotes: 2

Views: 5434

Answers (4)

Jason Brant
Jason Brant

Reputation: 182

You might consider using one of jQuery's plugins to simplify things somewhat. JSON Parsing and Stringifying in jQuery (as a plugin) This short article looks like it might help.

Upvotes: 0

simeonwillbanks
simeonwillbanks

Reputation: 1459

Since you're using jQuery's $.post, you can use the $_POST superglobal in your PHP. Its always a good idea to use a specific superglobal instead of $_REQUEST.

Take a look at the json_decode documentation: http://us.php.net/manual/en/function.json-decode.php

Since you're passing a second argument of TRUE, the returned value will be an associative array. Once your JSON is decoded, this will be the array structure:

array
  'rda' => 
    array
      0 => 
        array
          'id' => string '2' (length=1)
          'checked' => string 'true' (length=4)
      1 => 
        array
          'id' => string '1' (length=1)
          'checked' => string 'false' (length=5)

Now you can easily access members of the array!

echo $data_back['rda'][0]['id']; // 2

Here are both refactored scripts for reference.

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function() {
        var json_str = '{"rda":[{"id":"2","checked":"true"},{"id":"1","checked":"false"}]}';
        jQuery("a").click(function() {
            $.post("/test/parser", { data_to_send:json_str }, function (data_back) {
                $("div").html (data_back);
            });
        });
    });
    </script>   
</head>
<body>
<a href="#">post</a>
<!-- put decoded json in div -->
<div></div>
</body>
</html>


$data_back = json_decode($_POST['data_to_send'], true);
echo $data_back['rda'][0]['id']; // 2

Upvotes: 1

Andy E
Andy E

Reputation: 344537

It would just be

print $data_back->id_list[0]->id;

Objects in JSON format are converted to a native PHP object, arrays are converted to native arrays, unless you use set the $assoc parameter to true in the json_decode function, in which case the JSON data is converted to a php native associative array.

See http://us.php.net/json_decode

Upvotes: 1

eCaroth
eCaroth

Reputation: 531

json_decode with the second parameter set to true turns the JSON item into a php array.

So to access the element in your example, you'de use

$data_back['id_list'][0]['id']

Upvotes: 8

Related Questions