Treating associative JSON in PHP

0

I have a Java application:

    Gson gson = new Gson();
    String json = gson.toJson(pedido);

    DataServiceGenerator dataServiceGenerator = new DataServiceGenerator();
    Webservice service = dataServiceGenerator.createService(Webservice.class);
    Call<String> call = service.postJson(json);

Retrofit:

@POST("/servidor.php")
Call<String> postJson(@Body String pedido);

How can I retrieve this data to save to a table in php:

[{
"ped_data_hora": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"ped_hor_ent_producao": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"ped_hor_retorno": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"ped_hor_sai_producao": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"ped_hor_saida": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"ped_programado": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"ped_repique": {
    "year": 2018,
    "month": 6,
    "dayOfMonth": 4,
    "hourOfDay": 23,
    "minute": 47,
    "second": 51
},
"campo1": 1,
"campo2": 2,
"campo3": 3,
"campo4": 4
}]

I've tried this:

 $resultado=json_decode(json_encode( $_POST ), true );
... $resultado->ped_data_hora;
... $resultado[0]->ped_data_hora;
... $resultado['campo1'];

So:

$resultado=json_decode($_POST);

I have already checked and my JSON is valid, I just can not get the values;

    
asked by anonymous 05.07.2018 / 01:59

1 answer

0

It was solved with the help of colleague Anderson Carlos Woss , below:

$resultado = json_decode(json_decode($json), false );

Work with objects in this way:

$resultado[0]->campo1

And so:

$resultado[0]->campo1->sub_campo

And another important tip regarding dates, which is my case, to save in a Date () field in mysql for example, follows the hint:

$data = $resultado[0]->ped_hor_ent_producao;
$formatada = "{$data->year}/{$data->month}/{$data->day}";
    
05.07.2018 / 03:50