发送JSON stringify到PHP如何读取它


Send JSON stringify to PHP how to read it

在javascript中我使用JSON。将其字符串化生成下面的字符串

{"grid":[{"section":[{"id":"wid-id-1-1"},{"id":"wid-id-1-4"}]},{"section":[{"id":"wid-id-1-5"}]},{"section":[{"id":"wid-id-1-2"},{"id":"wid-id-1-3"}]}]}

我正在使用ajax调用php,我可以在php中获得字符串,因为它是,但之后我应该如何处理它。

下面是我的完整代码

 var mainArr = [];
        $('.sortable-grid')
                .each(function() {
                    var subArr = [];
                    $(this)
                            .children('.mygrid')
                            .each(function() {
                                var subObj = {};
                                subObj['id'] = $(this)
                                        .attr('id');
                                subArr.push(subObj);
                            });
                    var out = {
                        'section': subArr
                    };
                    mainArr.push(out);
                });
        var storePositionObj = JSON.stringify({
            'grid': mainArr
        });
$.ajax({
            url: __BASEURL + "php/update-details.php",
            data: {position: storePositionObj},

我最近开始学习PHP,不知道如何处理上面的字符串。

有谁能帮帮忙吗?

thank you

问候,莫娜

$json = json_decode($_POST['position']);

Try json_decode()

示例代码如下:

<?PHP
    $position = $_REQUEST['position'];
    print_r(json_decode($position));

只需使用json_decode()方法,然后您就可以像使用数组一样使用它。

可以像

一样使用
$data = json_decode($your_stringify_json);
var_dump($data['grid']);