当前位置:8288分类目录 » 站长资讯 » 编程 » 文章详细

json_encode参数/编码方式选择

来源:网络转载 浏览:25534次 时间:2023-10-18
<?php
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9",'这里是汉字');

echo "Normal: ",  json_encode($a), "\n";
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

$b = array();

echo "Empty array output as array: ", json_encode($b), "\n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";

结果:

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9","\u8fd9\u91cc\u662f\u6c49\u5b57"]

Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9","\u8fd9\u91cc\u662f\u6c49\u5b57"]

Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9","\u8fd9\u91cc\u662f\u6c49\u5b57"]

Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9","\u8fd9\u91cc\u662f\u6c49\u5b57"]

Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9","\u8fd9\u91cc\u662f\u6c49\u5b57"]

Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é","这里是汉字"]

All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é","这里是汉字"]


Empty array output as array: []

Empty array output as object: {}


Non-associative array output as array: [[1,2,3]]

Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}


Associative array always output as object: {"foo":"bar","baz":"long"}

Associative array always output as object: {"foo":"bar","baz":"long"}




8288分类目录声明:本站部分文章来源于网络,版权属于原作者所有。如有转载或引用文章/图片涉及版权问题,请联系我们处理.我们将在第一时间删除! 联系邮箱:tsk@qq.com

推荐网站

最新加入网站 TOP5

入站排行榜 TOP5