Handling malformed JSON in PHP

前端 未结 5 785
时光取名叫无心
时光取名叫无心 2021-01-12 01:58

I\'m trying to write a php script that handles data from a webservice that delivers \"json\" as a string. The problem is the string isn\'t really json; it\'s javascript. Spe

5条回答
  •  一生所求
    2021-01-12 02:30

    Depends on how complicated your data is :

    $output = "{desc:'User defined payload',asc:'whatever'}";
    
    function json_js_php($string){
    
        $string = str_replace("{",'{"',$string);
        $string = str_replace(":'",'":"',$string);
        $string = str_replace("',",'","',$string);
        $string = str_replace("'}",'"}',$string);
        return $string;
    
    }
    
    echo json_decode(json_js_php($output))->{'desc'}; 
    

    returns : User defined payload

提交回复
热议问题