Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
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: 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 Answers 4

up vote 0 down vote accepted

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.

share|improve this answer

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];
     ...
}
share|improve this answer

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

share|improve this answer
    
this returns the same (the pair name-value) in object form, but this is not what I need, look at my edited 1st post –  Michal Vašut Oct 17 '14 at 20:35

maybe take a look at this answer...

Basically you convert array (after json_decode) to object.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.