Treating associative JSON in PHP

Asked

Viewed 34 times

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 recover 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 it like this:

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

Thus:

$resultado=json_decode($_POST);

I already checked and my JSON is valid, I just can’t get the values;

  • What is the result you achieved in your two attempts?

  • null or failure to decode

  • ERROR: SQLSTATE[23000]: Integrity Constraint Violation: 1048 Column 'campo1' cannot be null

  • Okay, but this is an error in SQL, it has no relation to JSON reading.

  • But I confess that I am a little lost with the logic of treatment, since I have associative part in the json, and part not

  • yes, but either with an echo or directly to sql, has null result

  • when I give a var_dump(), returns this: array(0) {}

  • By the way, your JSON is not valid, no. "campo4": 4, this comma at the end cannot exist.

  • Correct, I removed some fields and left it, I will edit!

  • On second thought, where does JSON come in? If you receive the data via $_POST, he will already be a array. There is no point in converting it to JSON and then going back to array; as well as it makes no sense you decode JSON in a array. What exactly you want to make?

  • is part that I’m lost so, I’m sending a json client with data from a class converted to json, and I need to save this data to a mysql table on the server

  • And how are you sending this data? This information should be in the question, as well as this description of the problem you commented on.

  • the data goes exactly as I put it in json, the question was how to manipulate it from the server side, but I will complement then

Show 9 more comments

1 answer

0

It was resolved with the help of the colleague Anderson Carlos Woss, follow:

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

Give work with the objects in this way:

$resultado[0]->campo1

And so:

$resultado[0]->campo1->sub_campo

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.