tags:

views:

277

answers:

5

I'm trying to parse a simple JSON string, but I'm not used to do it from PHP. To do my testo I've wrote a js page where the data is created, sent to php page and returned back to do some test.

how can I access to json data from PHP and return them 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?

?>
A: 

JSON in PECL

Ignacio Vazquez-Abrams
It looks like he's using PHP 5.2 which has native json functions.
Andy E
yes, I'm already using it
Vittorio Vittori
+1  A: 

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

Andy E
+7  A: 

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']
eCaroth
thank you, it was exactly what I was looking for
Vittorio Vittori
+1  A: 

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"&gt;&lt;/script&gt;
    <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
simeonwillbanks
A: 

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.

Jason B-H