Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I map a JSON object to a PHP Class?

Tags:

json

oop

php

class

api

I'm building a REST API endpint that adds a company to a MySQL database. The client sends a POST request with an attached data package. The data package is a JSON object. Assume the JSON Company Object is formatted to exactly match the Company Class that the API uses.

How do I get the JSON Company Object data into the Company Class? It seems silly to instantiate a Company Object, json_decode() the JSON Object, then call dozens of set() methods.

It seems especially silly, since I'm planning on offering the same models in my client package to build the objects that get passed as JSON to my API, before being decoded, and mapped back into the same objects again.

Am I missing something? I'm constantly running up against things that seem redundant while building my API, but perhaps that's just what has to happen.

like image 668
T. Brian Jones Avatar asked Jun 19 '13 16:06

T. Brian Jones


People also ask

Can you map a JSON object?

You can map the data types of your business model into JSON by using the examples. Data in JSON is either an object or an array. A JSON object is an unordered collection of names and values.

How can access data from JSON in PHP?

Accessing JSON data as a PHP object By default the json_decode() function returns an object. To access the PHP object data, you use the object operator (->) after the object name, followed by the key of the key-value pair. This is the same as the name in the name-value pair in JSON object eg $data->firstName .

How pass JSON object in post request in PHP?

Send JSON data via POST with PHP cURLInitiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option. Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.

How show JSON data from HTML table in PHP?

To use PHP function file_get_contents () we can read a file and retrieve the data present in JSON file. After retrieving data need to convert the JSON format into an array format. Then with the use of looping statement will display as a table format.


2 Answers

Why don't you just make a method in the Company object that declares the variables for the object (you don't need to write a set method for each variable, just one that'll set all the variables).

//Why not write something like this in the class
function setFromJSON($json){
   $jsonArray = json_decode($json, true);
   foreach($jsonArray as $key=>$value){
      $this->$key = $value;
   }
}
like image 112
Joseph Szymborski Avatar answered Sep 20 '22 14:09

Joseph Szymborski


this one might help you.

 class ObjectMapper
    {

        protected string $modelClass='';

        public function __construct(string $modelClass){
        $this->modelClass = $modelClass;
}

        protected function mapObject(array $values){
            $object = new $this->modelClass();
            foreach ($values as $key=>$value){
                $mapperFunction = 'set'.$key;
                $object->{$mapperFunction}($value);
            }
            return $object;
        }

    }

like image 36
Florin Ciocirlan Avatar answered Sep 21 '22 14:09

Florin Ciocirlan