Recover JSON data via POST

Asked

Viewed 199 times

1

I have an Application that sends the strings via POST and recovers via JSON, but I am not able to separate the coordinates that arrive in an array.

Follow an example

$datas = file_get_contents('php://input');  
$encoded = json_decode($datas);

$id_usuario_rastreio =  $encoded->{'id_usuario'};
$id_grupo =  $encoded->{'id_grupo'};
$id_token =  $encoded->{'token'};

I need to recover now the locationList and separate to insert into the seat.

{
        "id_grupo": "20",
        "id_usuario": "21",
        "token": "0123456",
        "locationList":[{"date":"Feb 27, 2018 12:32:08 PM","latitude_rastreio":"-10.5267176","longitude_rastreio":"-10.5702931"},{"date":"Feb 27, 2018 12:33:08 PM","latitude_rastreio":"-10.5267176","longitude_rastreio":"-10.5702931"}]
}

Follow the test being performed at POSTMAN inserir a descrição da imagem aqui

1 answer

0

Well the function json_decode if you want to transform from json to array the syntax is:

$encode = json_decode($datas, TRUE);

From the moment you transform into an array you have to capture working as array by indexes/values, when you do $enconded-> is a way to access an object.

Your json....

{
        "id_grupo": "20",
        "id_usuario": "21",
        "token": "0123456",
        "locationList":[{"date":"Feb 27, 2018 12:32:08 PM","latitude_rastreio":"-10.5267176","longitude_rastreio":"-10.5702931"},{"date":"Feb 27, 2018 12:33:08 PM","latitude_rastreio":"-10.5267176","longitude_rastreio":"-10.5702931"}]
}

... convect to array stays:

array (
  'id_grupo' => '20',
  'id_usuario' => '21',
  'token' => '0123456',
  'locationList' => 
  array (
    0 => 
    array (
      'date' => 'Feb 27, 2018 12:32:08 PM',
      'latitude_rastreio' => '-10.5267176',
      'longitude_rastreio' => '-10.5702931',
    ),
    1 => 
    array (
      'date' => 'Feb 27, 2018 12:33:08 PM',
      'latitude_rastreio' => '-10.5267176',
      'longitude_rastreio' => '-10.5702931',
    ),
  ),
)

Soon locationList is an array of information and you must fetch the data as you work with arrays. There depends on what and how to capture, fetch all locationList catching with foreach etc.

Browser other questions tagged

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