<?php
$jsonData = '{ "programmers": [
    { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, 
    { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }, 
    { "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" } 
   ], 
  "authors": [ 
    { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, 
    { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, 
    { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } 
   ], 
  "musicians": [ 
    { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, 
    { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } 
   ] 
  } ';
$obj = json_decode($jsonData);
var_export($obj->authors[0]->firstName);
/**
 * 'Isaac'
 */
?>

解决方案 »

  1.   

    恩确实可以,其实我的代码不是这样的,为了问问题才弄了这样一个代码。
    echo $obj->authors[0]->firstName
    这样搞过不显示东西,不过这段代码确实可以,我再查查自己的。谢谢!
      

  2.   


    <?php
    $jsonData = '{ "programmers": [
        { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, 
        { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }, 
        { "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" } 
       ], 
      "authors": [ 
        { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, 
        { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, 
        { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } 
       ], 
      "musicians": [ 
        { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, 
        { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } 
       ] 
      } ';
    $arr = json_decode($jsonData,true);
    print_r($arr);die("|end");
    ?>