dq_609586475
dq_609586475
2014-07-25 13:39
浏览 464

将JSON stringify发送给PHP如何阅读它

In javascript i am using JSON.stringify it generate the below string

{"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"}]}]}

i am calling php using ajax, i can get the string in PHP as it is, but after that how i should process it.

Below is my full code

 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},

I recently started learning PHP, and don't know how to process this above string.

could any one please help.

thank you

regards, Mona

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutuobao9736
    doutuobao9736 2014-07-25 13:42
    已采纳
    $json = json_decode($_POST['position']);
    
    点赞 评论
  • doujiu3097
    doujiu3097 2014-07-25 13:42

    Try json_decode()

    sample code below:

    <?PHP
        $position = $_REQUEST['position'];
        print_r(json_decode($position));
    
    点赞 评论
  • douyu4822
    douyu4822 2014-07-25 13:48

    Just use json_decode() method and then you can use it like an array.

    You can use it like

    $data = json_decode($your_stringify_json);
    var_dump($data['grid']);
    
    点赞 评论

相关推荐