php - PHP无法解码用json_encode编码的JSON

  显示原文与译文双语对照的内容

在将大型(cca 60mb )geojson存储到数据库之前,我使用这个代码对它进行编码:


// controller


public function importZones () {



 ini_set('max_execution_time', '0');


 ini_set('memory_limit', '-1');


 ini_set("precision", -1);


 ini_set("serialize_precision", -1);



 $this->load->helper('url');



 $filePath = base_url('assets/zones/zone.geojson');



 $jsonStream = JsonMachineJsonMachine::fromFile($filePath,"/features");


 $this->PolygonModel->import($jsonStream)



}


// model


public function import ($jsonStream) {


 $import = [];


 foreach ($jsonStream as $name => $data) {


 // $coordinates is nested indexed array with possibly thousands of elements


 $coordinates = json_encode($data['geometry']['coordinates']); 


 $import['baz'][] = [


 'foo' => 'bar',


 'coordinates' => $coordinates


 ];


 }


 echo json_last_error_msg(); // gives no errors 



 // insert encoded data to db...


}



在从数据库选择字符串后,我使用这个代码来解码字符串


// decode and fix json if corrupted (huge arrays..)



private function decodeZoneCoords ($coordsJson) {



 $decoded = json_decode($coordsJson, true);



 if (!$decoded) {


 // some fixes I've found online


 $json = $coordsJson;


 // before removing cntrl I get the"Control character error, possibly incorrectly encoded" error


 $json = preg_replace('/[[:cntrl:]]/', '', $coordsJson);


 $json = preg_replace('/[[:^print:]]/', '', $coordsJson);


 $json = mb_convert_encoding($json,"UTF-8");


 $json = ltrim($json . '"' ."]", '1');


 if (0 === strpos(bin2hex($json), 'efbbbf')) {


 $json = substr($json, 3);


 }



 $decoded = json_decode($json, true);


 if (!$decoded) {


 $json = rtrim($json, ',"]') . '"]';


 $decoded = json_decode($json, true);


 }


 } 



 if (!$decoded) {


 echo"<pre>";


 var_dump( json_decode($json, true) ); // null


 echo"</pre>";


 echo json_last_error_msg(); //"syntax error"


 echo 'JSON ERROR(from zone):';


 echo '<hr>';


 echo $json;


 die();


}



 return $decoded;



}

试图解码时,从json_last_error_msg函数中得到语法错误消息,json_encode函数返回null。

编辑:

我刚刚意识到var_dumping字符串给我这个:string(65535)。

即使我使用文本字段存储编码的字符串,MySql是否会截断JSON?

时间:

65535是a的精确长度MySQL中的文本字段,如果json字符串的长度超过该长度,请使用mediumtext (16.777.215个字符)或longtext (294.967.295个字符)。

...