0

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

Michal Vašut
  • 187
  • 2
  • 8
  • I'm confused, you explicitly tell `json_decode()` to convert objects into arrays while you would like objects... You should start with removing the second argument from `json_decode`: http://php.net/manual/en/function.json-decode.php – jeroen Oct 17 '14 at 17:07
  • I watched the json_decode in manual and I understand how it works, maybe look at edited 1st post. – Michal Vašut Oct 17 '14 at 20:45

4 Answers4

2

To obtain an object instead of an array, use

$data = json_decode($jsonData, true);

instead of

$data = json_decode($jsonData, false);

Demo: https://eval.in/207258

PaulH
  • 2,918
  • 2
  • 15
  • 31
0

maybe take a look at this answer...

Basically you convert array (after json_decode) to object.

Community
  • 1
  • 1
Lord Zed
  • 750
  • 7
  • 28
  • no, I've maybe badly described it, I need array of products, where one product is object or associative array, look at sample result wher is product[0][name], product[0][url] and you'll undertand – Michal Vašut Oct 17 '14 at 17:13
  • `$objArray = array(); foreach($_POST['products'] as $product) { $objArray['name'] = $product[cor_projectProduct_name]; $objArray['url'] = $product[url]; $objArray['ean'] = $product[cor_projectProduct_ean]; $objArray['internalCode'] = $product[cor_projectProduct_internalCode]; ... }` is that what you need? – Néstor Oct 17 '14 at 18:12
0

serialize your form post is not needed. post limit can be changed in php settings if you need to handle long data. use unserialize to turn serialized data into key=>pair array

but for receiving and encoding in json_format (you are using json_decode without use first json_encode to format in json string whatever array).

checking at first view the returned array is valid for key names. but if you need only set the data attribut from template object, only you need to do $this->template->data = $_POST;

Edit:

to have an array of products:

$objArray = array();
foreach($_POST['products'] as $product) {
     $objArray['name'] = $product[cor_projectProduct_name];
     $objArray['url'] = $product[url];
     $objArray['ean'] = $product[cor_projectProduct_ean];
     $objArray['internalCode'] = $product[cor_projectProduct_internalCode];
     ...
}
Néstor
  • 570
  • 2
  • 8
  • 22
0

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.

Michal Vašut
  • 187
  • 2
  • 8