$data={"itemName":"Giordana","itemId":450,"itemPrice":45,"itemCategory":"Shirit",};
$result=json_decode($data,true);
YouneedtodoabovetwostepsinthecontrollerfunctionandthenifyouwanttoshowthatjsondataintheviewfileYoujustneedtopassthatarray$resulttoview.
$this->load->view('view_file',$data);
http://blog.csdn.net/gumincong/article/details/7738444
PHPjson_encode()函数介绍
在php中使用json_encode()内置函数(php>5.2)可以使用得php中数据可以与其它语言很好的传递并且使用它。
这个函数的功能是将数值转换成json数据存储格式。
08 | $jsonencode =json_encode( $arr ); |
程序运行结果如下:
json_encode函数中中文被编码成null了,Google了一下,很简单,为了与前端紧密结合,Json只支持utf-8编码,我认为是前端的Javascript也是utf-8的原因。
4 | 'title' =>iconv( 'gb2312' , 'utf-8' , '这里是中文标题' ), |
8 | echo json_encode( $array ); |
这个程序的运行结果为:
1 | { "title" : "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898" , "body" : "abcd..." } |
数组中所有中文在json_encode之后都不见了或者出现\u2353等。
解决方法是用urlencode()函数处理以下,在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。
02 | /************************************************************** |
04 | *使用特定function对数组中所有元素做处理 |
05 | *@paramstring&$array要处理的字符串 |
06 | *@paramstring$function要执行的函数 |
07 | *@returnboolean$apply_to_keys_also是否也应用到key上 |
10 | *************************************************************/ |
11 | function arrayRecursive(& $array , $function , $apply_to_keys_also =false) |
13 | static $recursive_counter =0; |
14 | if (++ $recursive_counter >1000){ |
15 | die ( 'possibledeeprecursionattack' ); |
17 | foreach ( $array as $key => $value ){ |
19 | arrayRecursive( $array [ $key ], $function , $apply_to_keys_also ); |
21 | $array [ $key ]= $function ( $value ); |
24 | if ( $apply_to_keys_also && is_string ( $key )){ |
25 | $new_key = $function ( $key ); |
27 | $array [ $new_key ]= $array [ $key ]; |
35 | /************************************************************** |
38 | *@paramarray$array要转换的数组 |
39 | *@returnstring转换得到的json字符串 |
42 | *************************************************************/ |
44 | arrayRecursive( $array , 'urlencode' ,true); |
45 | $json =json_encode( $array ); |
46 | return urldecode( $json ); |
这次成功了,运行结果如下:
1 | { "Name" : "希亚" , "Age" : "20" } |