wzkl.net
当前位置:首页 >> PHP 二维索引数组怎么转化成json字符串数组? >>

PHP 二维索引数组怎么转化成json字符串数组?

json_encode()就是将PHP数组转换成Json 如:$array = array("name" => "Eric","age" => 23); echo json_encode($array); 程序将打印出 : {“name”:”Eric”,”age”:23}

一、json_encode() [php] view plain copy 输出 [php] view plain copy {"a":1,"b":2,"c":3,"d":4,"e":5} 再看一个对象转换的例子: [php] view plain copy $obj->body = 'another post'; $obj->id = 21; $obj->approved = true; $obj->favorit...

应该是 json_decode($date,true); json_decode()正常转换后是对象,不是数组.

//数据库连接自己写$Rult = mysql_query('你的Sql语句');while( $row = mysql_fetch_assoc( $Rult ) ){ $Data[] = $row;} $Json['array'] = $Data;echo json_encode( $Json );//没有测试, 大致思路是这样

那不是乱码,那是utf8转码, php5.3之后可以加个参数避免转码,json_encode($arr, JSON_UNESCAPED_UNICODE);

$str = '{"hello": "world!"}'; //也可以是类似于'[1, 2, "3", 4]'的json数组$arr = json_decode($str, true); // 如果不加这个true,解析出来会是对象而不是数组print_r($arr);

可以通过json_encode的第二个参数实现,但php版本需要大于5.3.0 echo json_encode(array(0=>10,1=>11,2=>12), JSON_FORCE_OBJECT);//{"0":10,"1":11,"2":12}

结果如下[10,"Tom",true,"2015-10-15"]{"eid":10,"ename":"Tom","isMarried":true,"birthday":"2015-10-15"}

json_encode只能将utf8的字符转换成json字符串,如果你的代码格式不是utf8是无法转换的,会返回false,在转换之前,将数组中的值循环设置成utf8,遍历一次数组即可。然后再使用json_encode就可以了

js 代码 functionshowJSON() { varuser = { "username":"tom", "age":20, "info": {"tel":"123456","cellphone":"98765"}, "address": [ {"city":"shanghai","postcode":"201203"}, {"city":"suzhou","postcode":"200000"} ] } alert(user.usern...

网站首页 | 网站地图
All rights reserved Powered by www.wzkl.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com