使用PHP深入研究JSON

时间:2014-02-26 10:25:56

标签: php json curl

这是一项非常简单的任务,让我绝对难过!我通过CURL和PHP撤回一些JSON,并尝试从以下结构访问数据:

object(stdClass)[1]
  public 'maxResults' => int 43
  public 'resultList' => 
    array (size=43)
      0 => 
        object(stdClass)[2]
          public '@class' => string '' (length=64)
          public 'id' => int
          public 'version' => int 0
          public 'dateCreated' => string '2014-02-11T18:37:55.835+0000' (length=28)
          public 'dateModified' => null
          public 'locationId' => int
          public 'departmentId' => int
          public 'ownerCompanyId' => int
          public 'active' => boolean true
          public 'userId' => int
          public 'userName' => string '' (length=24)
          public 'externalCode' => null
          public 'employeeDetails' => 
            object(stdClass)[3]
              ...
          public 'chargeBandAllocationsIds' => 
            array (size=7)
              ...
          public 'personalRateChargeBandId' => 
            object(stdClass)[13]
              ...
          public 'employeeGroupIds' => 
            array (size=0)
              ...
          public 'isResource' => boolean false

(出于隐私原因,我删除了一些值)

现在我正在尝试var_dump使用var_dump(json_decode($result, false));,但是当我尝试使用var_dump(json_decode($result['resultList'], false));进入'resultList'数组时,我得到一个非法的字符串偏移错误。

1 个答案:

答案 0 :(得分:3)

$result是JSON字符串,您不能对字符串执行$result['resultList']。它只会在<{em> 之后成为一个结构json_decode。但是,您将其解码为 object ,而不是数组,因此无论如何都无法正常工作。

$data = json_decode($result);
var_dump($data->resultList);
var_dump($data->resultList[0]);
var_dump($data->resultList[0]->id);

foreach ($data->resultList as $employee) {
    var_dump($employee->id);
}