【问题标题】:parsing json for decode not working解析 json 以进行解码不起作用
【发布时间】:2018-02-13 16:15:52
【问题描述】:

当我尝试解析以解码它时,我从数据库中获取 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));

【问题讨论】:

  • 它没有显示,因为你没有打印它。
  • 试试$apiContent = json_decode($apiContent, TRUE); var_dump( $apiContent );
  • 当我回显 $apicontent 它显示时我也尝试过,但是当我尝试使用 json_decode 时它什么也没显示
  • 并且还尝试用''而不是""包装json字符串
  • 现在,它不是一个有效的字符串,因为里面有 unescape "

标签: php json


【解决方案1】:

您必须使用'' 而不是"" 包装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)

【讨论】:

    【解决方案2】:

    不知怎的,你把引号弄乱了。

    <?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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多