有这样个json.txt  文件 

"programmers": [ 
  { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, 
], 
"authors": [ 
  { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, 
], 
"musicians": [ 
  { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, 


现在需要读取类别的名字!! (即取到programmers、authors、musicians的值)
急!哪位大师知道呀?

解决方案 »

  1.   

    使用json_decode函数$filename = "json.txt";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    var_dump($contents);
    var_dump(json_decode($contents);
      

  2.   


    $fileName = 'json.txt';$fileContent = @file_get_contents($fileName);if($fileContent){
        $fileArray = json_decode($fileContent, true);
        var_dump(array_keys($fileArray));
    }
      

  3.   

    $json = json_decode(file_get_contents("json.txt"));
    $name = array_keys((array)$json);
    echo "<pre>".var_export($name,true)."</pre>";