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?

时间:

在將大型(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?

...