24

I don't work with php much and I'm a little fuzzy on object creation. I need to make a webservice request sending json and I think I have that part covered. Before I can submit the data I need to create a nested object. I was assuming this would be trivial based on my experience with ecma based scripting languages, but I'm finding the syntax to be difficult to navigate. The object I want to create is below.

{ "client": {
    "build": "1.0",
    "name": "xxxxxx",
    "version": "1.0"
    },
    "protocolVersion": 4,
    "data": {
        "distributorId": "xxxx",
        "distributorPin": "xxxx",
        "locale": "en-US"
    }
}

I've seen a lot of examples of flat objects, but I haven't found a minimal example for a nested object yet. What would be the php syntax for the object above? Is this an unusual thing to do in php?

7 Answers 7

47

this JSON structure can be created by following PHP code

$json = json_encode(array(
     "client" => array(
        "build" => "1.0",
        "name" => "xxxxxx",
        "version" => "1.0"
     ),
     "protocolVersion" => 4,
     "data" => array(
        "distributorId" => "xxxx",
        "distributorPin" => "xxxx",
        "locale" => "en-US"
     )
));

see json_encode

0
16

Hey here is a quick trick to manually convert complex JSONs into a PHP Object.

Grab the JSON example as you have:

{ "client": {
    "build": "1.0",
    "name": "xxxxxx",
    "version": "1.0"
    },
    "protocolVersion": 4,
    "data": {
        "distributorId": "xxxx",
        "distributorPin": "xxxx",
        "locale": "en-US"
    }
}

Search-Replace { to array(

Search-Replace : to =>

Search-Replace } to )

Done.

1
  • 1
    This is a risky approach. You could potentially replace characters unintentionally, ie. {"version":"1.0:beta"} would convert to array("version"=>"1.0=>beta"). Furthermore, what do you intend to do with the converted string? If you intend to eval() it, that could be very dangerous, especially if the input is provided by an unreliable source, see stackoverflow.com/questions/951373/when-is-eval-evil-in-php Aug 12, 2019 at 14:18
4

User array to get the correct format and then call echo json_encode(array)

           array( "client" => array(
    "build" => "1.0",
    "name" => "xxxxxx",
    "version" => "1.0"
 ),
 "protocolVersion" => 4,
 "data" => array(
    "distributorId" => "xxxx",
    "distributorPin" => "xxxx",
    "locale" => "en-US"
 ))
0
3
$client = new Client();
$client->information = new Information();
$client->information->build = '1.0';
$client->information->name = 'xxxxxx';
$client->information->version = '1.0';
$client->protocolVersion = 4;
$client->data = new Data();
$client->data->distributorId = "xxxx";
$client->data->distributorPin = "xxxx";
$client->data->locale = "en-US";

Perhaps something like the above? The client would hold two objects. Information and Data.

Edit Using json_encode, you would create this object as an array in PHP..

$clientObj = array('client'=> 
    array( array('build'=>'1.0','name'=>'xxxx', 'version'=>'1.0'), 

           'protocolVersion'=>4, 

           'data'=>array('distributorId' => 'xxxx', 'distributorPin' => 'xxxx', 'locale' => 'en-US')
);

print json_encode($clientObj);
2
  • Would I need to create the Client type as well?
    – Shane
    Apr 4, 2013 at 11:54
  • Yes, for on the fly creation definitely stdClass. Apr 4, 2013 at 12:00
2

We can also construct nested array and then do a json_encode to construct nested JSON.

For e.g:

{"User":
       {"username":"test",
        "address":"Posted value fro address field",
        "location":{
                     "id":12345
                    }
        }
}

Above output we can achieve by writing below php code:

<?php
$obj = array(
            'username'=>$lv_username,
            'address'=>$lv_address,
            'location'=>array('id'=>$lv_locationId)
    );
$data = '{"User":'. json_encode($obj) .'}';
echo $data;

?>

Hope it helps.

1

Use the in build function of PHP:

json_encode();

this will convert the array into JSON object.

1

You can use json_encode to encode a php array http://php.net/manual/en/function.json-encode.php

$theArray = array('client'= array('build'=>'1.0', 
                                'name'=>'xxxxx', 
                                'version'=>'1.0'
                               ), 
                'protocolVersion'=> 4, 
                'data'=> array('distributorId'=>'xxxx', 
                               'distributorPin'=>'xxxx', 
                               'locale'=>'en-US' 
                               ) 
                );

$theObj = json_encode($theArray);

hopefully this helps..

posted it, then seen loads of answers already! :|

Your Answer

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

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