doubang4881 2014-10-17 16:57
浏览 36
已采纳

javascript和php反序列化中的JSON序列化

Hello I have problem with deserialization of JSON serialized in javascript. I have form on webpage and every row is one product (inputs, selects, checkboxes in form):

name, price, total_count, ...
name2, price2, total_count2, ...
...

I take a form and serialize it with javascript (function ajaxLoad is normal shortened jQuery fuction $.ajax(...) and it works at another places correctly )

var form = $('#myForm');

form.submit(function(event){
    event.preventDefault();
    ajaxLoad(
        form.attr('action'),
        form.parent(),
        {jsonData : JSON.stringify(form.serializeArray())}
        );
    });

in php, the data are received and my code is following:

$data = json_decode($jsonData, true);
$this->template->data = print_r($data,1);

it returns something like that:

Array
(
    [0] => Array
        (
            [name] => products[0][cor_projectProduct_name]
            [value] => 1ks Replika Kádnerova jáchymovského tolaru stand BJ
        )

    [1] => Array
        (
            [name] => products[0][url]
            [value] => http://some-nice-url.cz
        )

    [2] => Array
        (
            [name] => products[0][cor_projectProduct_ean]
            [value] => 
        )

    [3] => Array
        (
            [name] => products[0][cor_projectProduct_internalCode]
            [value] => 
        )

    [4] => Array
        (
            [name] => products[0][cor_project_id]
            [value] => 6
        )

    [5] => Array
        (
            [name] => products[0][cor_projectProduct_keywordAllowed]
            [value] => 
        )
...

but I would like to have array of objects.

I've tried to serialize the form with form.serialize as well, but returned result was even worse - urlencoded string, that I couldn't decode.

When I've tried to send data via POST method as array and then read it in php from $_POST, it worked but some data was lost due to POST limitation, so it is better to post it as serialized string, but I don't know how and how to deserialize it in php.

EDIT: OK, maybe it was badly explained, what I need as a result is array of Objects:

Array
(
    [0] => stdClass Object
        (
            [cor_projectProduct_name] => 1ks Replika Kádnerova jáchymovského tolaru stand BJ
            [url] => http://www.ceske-mince.cz/ceska_mincovna/1997/replika-kadnerova-jachymovskeho-tolaru-stand/
            [cor_projectProduct_ean] => 
            [cor_projectProduct_internalCode] =>
            [cor_project_id] => 6
            [cor_projectProduct_keywordAllowed] =>
            ...
        )
    [1] => stdClass Object
        (
            [cor_projectProduct_name] => 1ks Replika Kádnerova jáchymovského tolaru stand BJ
            ...
        )
    ...

or the same with associative arrays, it does not matter

  • 写回答

4条回答 默认 最新

  • douanrang4728 2014-10-20 15:30
    关注

    OK, I figured out myself how to do it:

    in javascript, all the same except line with data must be changed to:

    {jsonData : JSON.stringify(form.serialize())}
    

    and in PHP I can access to accepted data after calling parse_str() function, so it would be something like this:

    public function actionSubmitedImportedProducts($jsonData){
        parse_str(json_decode($jsonData));
    
        $this->template->data = print_r($products,1);
    }
    

    OMG, so lame :-D.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功