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

2
  • 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 Commented Oct 17, 2014 at 17:07
  • I watched the json_decode in manual and I understand how it works, maybe look at edited 1st post. Commented Oct 17, 2014 at 20:45

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

this returns the same (the pair name-value) in object form, but this is not what I need, look at my edited 1st post
0

maybe take a look at this answer...

Basically you convert array (after json_decode) to object.

2 Comments

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
$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?
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];
     ...
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.