Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create nested JSON object in php?

Tags:

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?

like image 889
Shane Avatar asked Apr 04 '13 11:04

Shane


People also ask

How to get nested JSON data in PHP?

Firstly read the contents of the text file into a string variable using the file_get_contents() function and then use json_decode() function to convert the JSON string to a PHP variable. $filepath = './persons. txt'; $json_string = file_get_contents($filepath); $json = json_decode($json_string, true);

How to create complex JSON in PHP?

We can also construct nested array and then do a json_encode to construct nested JSON. 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":'.

What is a nested JSON?

Nested JSON is simply a JSON file with a fairly big portion of its values being other JSON objects. Compared with Simple JSON, Nested JSON provides higher clarity in that it decouples objects into different layers, making it easier to maintain.

How to convert a request to JSON in PHP?

PHP File explained: 1 Convert the request into an object, using the PHP function json_decode (). 2 Access the database, and fill an array with the requested data. 3 Add the array to an object, and return the object as JSON using the json_encode () function. More ...

How to decode a JSON object into an array in PHP?

The json_decode () function is used to decode a JSON object into a PHP object or an associative array. The json_decode () function returns an object by default. The json_decode () function has a second parameter, and when set to true, JSON objects are decoded into associative arrays.

How to handle JSON in PHP?

PHP has some built-in functions to handle JSON. Objects in PHP can be converted into JSON by using the PHP function json_encode():

What is the difference between JSON_encode () and JSON_DECODE () function?

The json_encode () function is used to encode a value to JSON format. The json_decode () function is used to decode a JSON object into a PHP object or an associative array. The json_decode () function returns an object by default.


2 Answers

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

like image 149
Ejaz Avatar answered Sep 25 '22 02:09

Ejaz


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.

like image 42
Guillermo Tallano Avatar answered Sep 23 '22 02:09

Guillermo Tallano