From the Android application I make a POST request with a JSON string to my server (php). How do I handle JSON on the server? Can some example or link ?!

  • Link / example for what? - u_mulder
  • 3
    In short, json_decode(file_get_contents('php://input'), true); - vp_arth

1 answer 1

 $json = $_POST['ключ']; $arr = json_decode($json, true); // Вы получаете ассоциативный массив, в котором ключи есть ключи объекта. Т.е. массив будет повторять структуру Вашего json. 

Here is an example.

 <?php $json = '{"test":[{"a": "aaa", "b": "bbb"},{"a": "AAA", "b": "BBB"}]}'; $arr = json_decode($json, true); foreach($arr['test'] as $value) { echo $value["a"] . " "; echo $value["b"] . " "; } ?> 
  • Usually, if a json is sent in a Post request, it is not a form-urlencoded field, but is transferred entirely in the request body with Content-type: application/json . Although anything can happen. - vp_arth
  • If json is transmitted in the body, then it will be a string in the variable $ _POST - ilyaplot
  • This method will be equivalent to file_get_contents? - Vlad