function encode($array)
{
if(version_compare(PHP_VERSION,"5.4.0","<")){
$str = json_encode($array);
$str = preg_replace_callback("-sharp\\\u([0-9a-f]{4})-sharpi",function($matchs){
return iconv("UCS-2BE", "UTF-8", pack("H4", $matchs[1]));
},$str);
return $str;
}else{
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
}
this is a function for converting Chinese;
I check out the data from the database and convert it to json format.
this is js; in html
function test(){
var id = $(".father option:selected").val();
$.ajax({
url: "regulation_type.php",
type: "post",
data: {id:id , act:"ajax"},
success:function(msg){
//var str = "{"id":"2","title":"","parent_id":"1","chapter":""}";
//obj = JSON.parse(str);
console.log(msg);
str = msg.replace("[{","{");
str_1 = msg.replace("}]","}");
console.log(str_1);
output msg, directly to get [{"id": "2", "title": "Criminal Law Task", "parent_id": "1", "chapter": "Chapter I"}]
, with a []
, I thought it was an array. Using typeof
, I looked at the string, obj = JSON.parse (json);.
output obj.id
has no value, and then write {"id": "2", "title": "Criminal Law Task", "parent_id": "1", "chapter": "Chapter I"}
; Use obj = JSON.parse (json);
to output obj.id
, and then want to get rid of the outside []
, but you can"t get rid of it. str = msg.replace ("[{","{"); str_1 = msg.replace ("}],"}");
the one on the right can be removed, but the one on the left can"t be removed, is there any big god who knows the reason, solve it, online, etc.!