解析json解码不工作

时间:2018-02-13 16:15:52

标签: php json

当我尝试解析解码时,我从数据库获取json字符串

$apiContent = "[{"caseid":2,"fullname":"df","businessname":"asdf","phonenumber":"12345678","activity":"sdf","province":7,"wilayat":"adfasd","description":"sfasdf","casedate":{"date":"2018-02-08 11:39:19.000000","timezone_type":3,"timezone":"Asia\/Muscat"}}]"

print_r(json_decode($apiContent, TRUE));

2 个答案:

答案 0 :(得分:2)

您必须使用''而非""

包装json字符串

使用"不会使您的字符串有效。

//            -------- Use ' instead of " (Also use this on the end of the string )
//            v
$apiContent = '[{"caseid":2,"fullname":"df","businessname":"asdf","phonenumber":"12345678","activity":"sdf","province":7,"wilayat":"adfasd","description":"sfasdf","casedate":{"date":"2018-02-08 11:39:19.000000","timezone_type":3,"timezone":"Asia\/Muscat"}}]';
$apiContent = json_decode($apiContent, TRUE); 
var_dump( $apiContent );

这将导致:

array (size=1)
  0 => 
    array (size=9)
      'caseid' => int 2
      'fullname' => string 'df' (length=2)
      'businessname' => string 'asdf' (length=4)
      'phonenumber' => string '12345678' (length=8)
      'activity' => string 'sdf' (length=3)
      'province' => int 7
      'wilayat' => string 'adfasd' (length=6)
      'description' => string 'sfasdf' (length=6)
      'casedate' => 
        array (size=3)
          'date' => string '2018-02-08 11:39:19.000000' (length=26)
          'timezone_type' => int 3
          'timezone' => string 'Asia/Muscat' (length=11)

答案 1 :(得分:1)

不知怎的,你搞乱报价。

<?php 

  $apiContent = '[{"caseid":2,"fullname":"df","businessname":"asdf","phonenumber":"12345678","activity":"sdf","province":7,"wilayat":"adfasd","description":"sfasdf","casedate":{"date":"2018-02-08 11:39:19.000000","timezone_type":3,"timezone":"Asia\/Muscat"}}]';

  $t = json_decode($apiContent, TRUE);

  var_dump($t);