Jump to content

help with converting json array and objects into php array and objects.


s0c0

Recommended Posts

I am using the Services_JSON class since we are running PHP 4.3.  I have successfully converted simple arrays from javascript to json to php and then back again in fiddling around with this, but now I am actually trying to do something useful.

 

In javascript I have built an array of objects like so:

 

productsArr[prodCount] = new product_Constructor(id,product,price,fluxCost,discountable,qty,taxable,1);

 

the "prodCount" variable is simply an auto-incrementing integer used for unique array indexes and the "product_Constructor" is just the constructor for my product object, the object is stored in and referenced by the array index.  Don't concern yourself with where I get the variables I pass into the constructor as this works fine.

 

Using an Ajax HTTP POST I pass this array over server side, the array ends up looking like this:

[null,{"id":"4534","name":"MaMa Lotion Trial Pack","price":"8.95","fluxCost":"3.3","discountable":"0","qty":"1","taxable":"0","inCart":1},{"id":"290","name":"SkinCareRx Kit for Men","price":"65.00","fluxCost":"32.5","discountable":"0","qty":"1","taxable":"0","inCart":1}]

 

When it hits the php side I use the following code:

 

		$productsArr = $this->json->decode(stripcslashes($productsJSON); // build array from json text
	foreach($productsArr as $prod)
	{
		echo $this->json->decode($prod);
	}

 

But I get nothing.  If I echo out the json text my PHP function receives it mirrors what was sent.  If I do a print_r on the array it shows empty elements.  Not even the json text is present in anywhere in the array.  Why is this?

 

Now before you question me, an array of objects was not my first choice.  My initial choice was a multidimensional array to be converted into json text and then passed into PHP.  I had problems converting a multidimensional array into json text with the latest version the json.js file though.  Any help is appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.