Shane
Shane

Reputation: 5151

create nested JSON object in php?

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?

Upvotes: 26

Views: 57498

Answers (7)

Guillermo Tallano
Guillermo Tallano

Reputation: 177

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.

Upvotes: 16

MSVKC
MSVKC

Reputation: 21

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.

Upvotes: 2

ilikemypizza
ilikemypizza

Reputation: 347

$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);

Upvotes: 4

Marty
Marty

Reputation: 4657

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! :|

Upvotes: 1

Ejaz
Ejaz

Reputation: 8872

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

Upvotes: 48

Code Lღver
Code Lღver

Reputation: 15603

Use the in build function of PHP:

json_encode();

this will convert the array into JSON object.

Upvotes: 1

Devesh
Devesh

Reputation: 4550

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"
 ))

Upvotes: 4

Related Questions