1.json_encode basic Usage: array to string
<?php$arr = Array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5); Echo Json_encode ($arr);? > The above routines will output: {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
2.json_decode Basic usage: string to array
<?php $json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '; Var_dump (Json_decode ($json));?> output array{["a"] + int (1) ["B"] + int (2) ["C"] = + int (3) ["D"] = Int (4) [ "E"] = Int (5)
3.json_encode will be escaped when exporting Chinese characters.
$arr 4 = Array (' t1 ' = ' Test Chinese characters ', ' t2 ' = ' www.jd.com ', ' t3 ' = ' 123456789 ', ' t4 ' = ' = ' [email protected]#$%^&* ", ' t5 ' = ' Chinese +www.jd.com+465454 ');p rint_r (Json_encode ($arr 4)); exit;
Output: ( Chinese characters will be escaped )
{"T1": "\u6d4b\u8bd5\u4e0b\u4e2d\u6587\u6c49\u5b57", "T2": "www.jd.com", "T3": "123456789", "T4": "[Email protected]#$ %^&* "," T5 ":" \u6c49\u5b57+www.jd.com+465454 "}
4. How to ensure that Json_encode is not escaped
$arr 5 = Array (' T1 ' =>urlencode (' Test Chinese characters ')); Key 1print_r (UrlDecode (Json_encode ($arr 5))); Focus 2
Output Result:
{"T1": "Chinese characters under test"}
5.urlencode,json_encode,urldecode use
$arr 6 = UrlEncode (' Chinese characters under test ');p Rint_r ($arr 6);
Print_r (Json_encode ($arr 6));
Print_r (UrlDecode ($arr 6));
Print_r (UrlDecode (Json_encode ($arr 6));
Output Result:
%e6%b5%8b%e8%af%95%e4%b8%8b%e4%b8%ad%e6%96%87%e6%b1%89%e5%ad%97 //$arr 6 output
"%e6%b5%8b%e8%af%95%e4%b8%8b%e4%b8%ad%e6%96%87%e6%b1%89%e5%ad%97" //Json_encode output
Test Chinese Kanji //UrlDecode Output results
"Chinese characters under test" //Urldecode+json_encode Output results
6.Jquery Fetching PHP Json_encode content
PHP code
$arr = Array ( [goods_id] = 1 [GOODS_SN] = LX000000 [Goods_name] = free trade Zone straight hair German Aptamil love his beauty infant formula pre section 800g "2 cans Start") echo Json_encode ($arr)
Output format
{"id": "1", "user_id": "1", "goods_id": "1", "Goods_stock": "6", "GOODS_SN": "LX000000", "Shop_price": 168}
JS Output JSON format content
function T (n,v) { res = ajax.call (' user.php?act=depot_id ', ' id=1 ', null, "GET", "JSON", false); var goods_title = "#goods_title_" +N; var goods_name = "#goods_name_" +N; var goods_price = "#goods_price_" +N; $ (Goods_title). Val (res.goods_title); $ (Goods_name). Val (res.goods_name); $ (Goods_price). Val (res.goods_price); alert (res.goods_name); document.getElementById (div). Value = ' one '; }
7.json_decode has stdclass Object, solution Json_decode ($s, true),
$s = ' [{' goods_id ': ' 2 ', ' Goods_num ': 2, ' goods_name ': ' u8377u5170nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 "},{" goods_id ":" 2 "," Goods_num ": 2," Goods_name ":" U8377u5170nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 "}] ';
F=jsondecode (s);
Output:
Array ( [0] = = StdClass Object ( [goods_id] = 2 [Goods_num] + 2 [goods_name] = = u8377u5170nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ) [1] = = StdClass Object ( [ GOODS_ID] = 2 [Goods_num] = 2 [Goods_name] = u8377u5170nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ))
Workaround:
F=jsondecode (s,true);
Print_r ($f);
Output:
Array ( [0] = = Array ( [goods_id] = 2 [Goods_num] = 2 [Goods_name] = = u8377u5170nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ) [1] = = Array ( [goods_id] = > 2 [Goods_num] and 2 [goods_name] = u8377u5170nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ))
The simplest way to escape the Json_unescaped_unicode 8.json Kanji
Return Json_encode ($arr, json_unescaped_unicode); PHP 5.3 or higher can be used (Json_unescaped_unicode)
Transferred from: http://www.cnblogs.com/wesky/p/4590953.html
Usage of Json_encode and Json_decode in PHP