A
A
atis //2016-11-02 17:15:44
PHP
atis //, 2016-11-02 17:15:44

How to collect an object from JSON in PHP?

Hello.
There is json, for example: It is {"name":"Jack.M","birthDate":"12.12.1991"}
necessary to collect an object of the JsonPerson class from it.
So far, only a simple constructor and implementation in it are on my mind.

class JsonPerson
{
    public $name;
    public $birthDate;

    public function __construct($json)
    {
        // realization
    }
}

It would be desirable the interface for all this business. But the interface is only for serialization.
JsonSerializable:: jsonSerialize
Who will advise what options?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2016-11-02
@atis2345

json_encode to do from the object initially, then the structure and class should be preserved.
Well, either in the class createFromJson method to do, in it json_decode and filling the object with data.
There seems to be no magical third option.

A
Alexander Kubintsev, 2016-11-02
@akubintsev

You need jms/serializer. symfony.com/doc/current/components/serializer.html

A
Alexander Wolf, 2016-11-02
@mannaro

php.net/manual/en/function.json-decode.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question