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?
– Woss
null or failure to decode
– Rodrigo
ERROR: SQLSTATE[23000]: Integrity Constraint Violation: 1048 Column 'campo1' cannot be null
– Rodrigo
Okay, but this is an error in SQL, it has no relation to JSON reading.
– Woss
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
– Rodrigo
yes, but either with an echo or directly to sql, has null result
– Rodrigo
when I give a var_dump(), returns this: array(0) {}
– Rodrigo
By the way, your JSON is not valid, no.
"campo4": 4,
this comma at the end cannot exist.– Woss
Correct, I removed some fields and left it, I will edit!
– Rodrigo
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?– Woss
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
– Rodrigo
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.
– Woss
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
– Rodrigo
Let’s go continue this discussion in chat.
– Woss