JSON Objects to objects...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

JSON Objects to objects...

Post by hybris »

Hi,

I have a class with some properties like id, name and stuff and some functions.
When i JSON encode it and send to my program I JSON decode it and get an object with the same properties as my class but without the functions of the class...
Is there a smart way of turning this JSON object to an object of my class or do i have to copy property for property?

As I do now:

Code: Select all

                    $product = new Models\Product;
                    $obj = json_decode($_POST["product"]);
            
                    $product->name = $obj->name;
                    $product->grossweight = $obj->grossweight;
                    $product->gw_unit = $obj->gw_unit;
                    $product->netweight = $obj->netweight;
                    $product->nw_unit = $obj->nw_unit;             
This works just fine but is there a better way to do it?

Thanks :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: JSON Objects to objects...

Post by Christopher »

That is probably the most straightforward way to do it. I would create a importJson() method in Models\Product and put that code into the class.
(#10850)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: JSON Objects to objects...

Post by hybris »

Christopher wrote:That is probably the most straightforward way to do it. I would create a importJson() method in Models\Product and put that code into the class.
I was so amazed when it just worked I almost fell off my chair.. I never used JSON so I thought I would spend weeks before I get it all to work and then 2 or 3 tries (5-10 mins or so) later it all worked haha.

Excellent suggestion to put it inside the class, will do that asap. Thanks :)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: JSON Objects to objects...

Post by hybris »

Hmm it works great for single dimention arrays but for deeper arrays it convert it to stdClass Object

Like:
[ingredients] => Array ( [0] => Högpastöriserad mjölk [1] => mjölkprotein [2] ......)
which is OK (Single dimention)

[nutrition_info] => stdClass Object ( [fat] => stdClass Object ( [1.50] => g ) [fat saturated] => stdClass Object ( [1.10] => g ) [carbohydrate] => stdClass Object ( [4.60] => g ).....)
which is NOT OK..

Why does it suddenly treat next level as objects?

I typecasted it back to array like this:

Code: Select all

foreach ($obj->nutrition_info as $key => $value) {
            $this->nutrition_info[$key] = (array) $value;      //Typecasting std_object to array.
        }
which seems to work and produce the result I want but I'm not sure if it is the correct way of doing it and it feels so so since I don't know why the arrays turned into objects in the first place?

Here is the result after the typecast:
[nutrition_info] => Array ( [fat] => Array ( [1.50] => g ) [fat saturated] => Array ( [1.10] => g ) [carbohydrate] => Array ( [4.60] => g ) ......)

Do I need to worry?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: JSON Objects to objects...

Post by Christopher »

The reason that the arrays are defined as objects is due to json_decode(). That is the standard behavior. There is a second parameter with some flags that may help control this. However, what you are doing is fine, and converts it to an array which is what you want.

FYI, objects and arrays are similar in PHP, so you could also experiment with doing something like this:

Code: Select all

$merged_obj = (object) array_merge((array) $product, (array) $obj);
(#10850)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: JSON Objects to objects...

Post by hybris »

Aha ok thanks :)
Post Reply