PHP: parse JSON from jQuery

☆樱花仙子☆ 提交于 2021-02-04 16:30:51

问题


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?

?>

回答1:


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']



回答2:


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




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/2065510/php-parse-json-from-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!