Users Pricing

forum

Home Forums How do I map a JSON object to a PHP Class? – MindStick

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

Anonymous User 4787 20 Jun 2013
Hi Developers,
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.

1 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md