How does PHP calculate JSON length

for example, how to judge whether the length of JSON reaches three. If only MCVersion,JavaVersion lacks Server, it indicates that JSON is incomplete. How to implement

{"MCVersion":"v1.10.0","JavaVersion":"v1.8","Server":"Linux"}
Jun.04,2021

The

json object is the structure of a class array, and the sizeof function can be used to determine the length

.
$data1=json_decode('{"MCVersion":"v1.10.0","JavaVersion":"v1.8","Server":"Linux"}', true);

var_dump($data1);
echo sizeof($data1);

will output

array(3) {                                                                                   
  ["MCVersion"]=>                                                                            
  string(7) "v1.10.0"                                                                        
  ["JavaVersion"]=>                                                                          
  string(4) "v1.8"                                                                           
  ["Server"]=>                                                                               
  string(5) "Linux"                                                                          
}                                                                                            
3

Note that the second parameter of json_decode will return array type if you want to use true,. You can use sizeof, otherwise it will return stdClass.

Menu