PHP中json_decode()和json_encode()的使用方法

上述例程会输出:

1.json_decode() 

Report a bug 参数

Report a bug 范例

json_encode 
(PHP
5 >= 5.2.0, PECL json >= 1.2.0) 

参数

<?php 
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); 

echo json_encode($arr); 
?>

json_decode
— 对 JSON 格式的字符串举行编码 

Example #1 A json_encode() 的例子

结果为:

json_encode
— 对变量举行 JSON 编码 

<?php
echo “一而再一连数组”.PHP_EOL;
$sequential = array(“foo”, “bar”, “baz”, “blong”);
var_dump(
$sequential,
json_encode($sequential)
);

范例

Report
a bug 返回值 
编码成功则赶回叁个以
JSON 方式表示的 string 可能在曲折时回来 FALSE 。 

echo json_decode($data,true);

上例将出口:

参数 

echo “Empty array output as array: “, json_encode($b), “n”;
echo “Empty array output as object: “, json_encode($b,
JSON_FORCE_OBJECT), “nn”;

json_decode对JSON格式的字符串实行编码而json_encode对变量举办 JSON
编码,须求的心上人能够参谋下。

小说由爱资料原创本文地址:

Array ( [0] => stdClass Object ( [Name] => a1 [Number] =>
123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object (
[Name] => a1 [Number] => 123 [Contno] => 000 [QQNo]
=> ) [2] => stdClass Object ( [Name] => a1 [Number] =>
123 [Contno] => 000 [QQNo] => ) )

object(stdClass)#1 (5) { 
["a"] => int(1) 
["b"] => int(2) 
["c"] => int(3) 
["d"] => int(4) 
["e"] => int(5) 
} 

array(5) { 
["a"] => int(1) 
["b"] => int(2) 
["c"] => int(3) 
["d"] => int(4) 
["e"] => int(5) 
}

$data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]'; 
echo json_decode($data);

options 
由以下常量组成的二进制掩码:
JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS,
JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES,
JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. 

1.json_decode()

<?php 
$a = array('<foo>',"'bar'",'"baz"','&blong&', "xc3xa9"); 

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), "nn"; 

$b = array(); 

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

$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), "nn"; 

$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), "nn"; 
?>

Report
a bug 参数 

可是自个儿的数额是要透过POST发送的,并不在HTTP
的head中,而在message-body里,所以不受影响。

能够看来json_encode()和json_decode(卡塔尔(قطر‎是编写翻译和反编写翻译进度,注意json只选取utf-8编码的字符,所以json_encode(State of Qatar的参数必得是utf-8编码,不然会赢得空字符可能null。

value 
待编码的
value ,除了resource 类型之外,可感到其余数据类型 

Report a bug 范例

Report a bug 返回值
编码成功则赶回一个以 JSON 格局表示的 string 或然在波折时回来 FALSE 。

该函数只好接收UTF-8 编码的数码 

json_encode — 对变量进行 JSON 编码

能够看来
json_decode($data,trueState of Qatar输出的二个关联数组,由此可以预知json_decode($data)输出的是指标,而json_decode(“$arr”,true卡塔尔国是把它强逼生成PHP关联数组.

Report
a bug 说明 
string
json_encode ( mixed $value [, int $options = 0 ] ) 
返回
value 值的 JSON 形式 

json_encode 只好肩负utf-8格式的多少

结果为:

说明 
mixed
json_decode ( string $json [, bool $assoc ] ) 
经受四个JSON 格式的字符串况兼把它转换为 PHP 变量 

若果是华语的话将在注意了

Report a bug 说明
string json_encode ( mixed $value [, int $options = 0 ] )
返回 value 值的 JSON 形式

2.json_encode() 

 代码如下

Example #2 json_encode(State of Qatar 函数中 options 参数的用法

assoc 
当该参数为
TRUE 时,将回到 array 而非 object 。 

在网络找到风流倜傥种减轻方法:

1.json_decode()

json 
待解码的
json string 格式的字符串。 

专门的学问的Base64并不合乎直接放在URAV4L里传输,因为UENVISIONL编码器会把规范Base64中的“/”和“+”字符变为形如“%XX”的花样,而这么些“%”号在存入数据库时还供给再开展转变,因为ANSI
SQL中已将“%”号用作通配符。

Example #1 json_decode() 的例子

json_decode 
(PHP
5 >= 5.2.0, PECL json >= 1.2.0) 

连天数组
array(4) {
[0]=>
string(3) “foo”
[1]=>
string(3) “bar”
[2]=>
string(3) “baz”
[3]=>
string(5) “blong”
}
string(27) “[“foo”,”bar”,”baz”,”blong”]”

json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

php中json_decode()和json_encode()

echo json_decode($data,true);

范例

该函数只好选取 UTF-8 编码的多寡

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

结果:

奥门新浦京官方网站,array(5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}

连续数组 
array(4) { 
[0]=> 
string(3) "foo" 
[1]=> 
string(3) "bar" 
[2]=> 
string(3) "baz" 
[3]=> 
string(5) "blong" 
} 
string(27) "["foo","bar","baz","blong"]" 

非连续数组 
array(4) { 
[1]=> 
string(3) "foo" 
[2]=> 
string(3) "bar" 
[3]=> 
string(3) "baz" 
[4]=> 
string(5) "blong" 
} 
string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}" 

删除一个连续数组值的方式产生的非连续数组 
array(3) { 
[0]=> 
string(3) "foo" 
[2]=> 
string(3) "baz" 
[3]=> 
string(5) "blong" 
} 
string(33) "{"0":"foo","2":"baz","3":"blong"}"

$obj->Name= 'a1';$obj->Number ='123'; 
$obj->Contno= '000'; 
echo json_encode($obj);

Normal: [“<foo>”,”‘bar'”,””baz””,”&blong&”,”u00e9″]
Tags: [“u003Cfoou003E”,”‘bar'”,””baz””,”&blong&”,”u00e9″]
Apos: [“<foo>”,”u0027baru0027″,””baz””,”&blong&”,”u00e9″]
Quot: [“<foo>”,”‘bar'”,”u0022bazu0022″,”&blong&”,”u00e9″]
Amp: [“<foo>”,”‘bar'”,””baz””,”u0026blongu0026″,”u00e9″]
Unicode: [“<foo>”,”‘bar'”,””baz””,”&blong&”,”é”]
All:
[“u003Cfoou003E”,”u0027baru0027″,”u0022bazu0022″,”u0026blongu0026″,”é”]

assoc
当该参数为 TRUE 时,将重回 array 而非 object 。

$obj->Name= ‘a1′;$obj->Number =’123’;
$obj->Contno= ‘000’;
echo json_encode($obj);

以上例程会输出:

后来请教了外人,仍是可以用base64编码,可是base64编码不得以放在ULX570L中,百度是那样表达的:

{"Name":"a1", 
"Number":"123", 
"Contno":"000" 
}

Example #3 三番若干次与非三番五次数组示例

参数

能够看来json_encode()和json_decode(卡塔尔(قطر‎是编写翻译和反编写翻译进度,注意json只接受utf-8编码的字符,所以json_encode(卡塔尔国的参数必需是utf-8编码,不然会得到空字符恐怕null

<?php 
echo "连续数组".PHP_EOL; 
$sequential = array("foo", "bar", "baz", "blong"); 
var_dump( 
$sequential, 
json_encode($sequential) 
); 

echo PHP_EOL."非连续数组".PHP_EOL; 
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong"); 
var_dump( 
$nonsequential, 
json_encode($nonsequential) 
); 

echo PHP_EOL."删除一个连续数组值的方式产生的非连续数组".PHP_EOL; 
unset($sequential[1]); 
var_dump( 
$sequential, 
json_encode($sequential) 
); 
?>

能够见见经过json_decode(卡塔尔(قطر‎编写翻译出来的是指标,现在出口json_decode($data,true)试下

如上例程会输出:

{“item1″:1,”item2″:”u4e2du6587”}
{“item1″:1,”item2″:”中文”}
{“item1″:1,”item2″:”u4e2du6587”}
“u80e5”

{"a":1,"b":2,"c":3,"d":4,"e":5}

 代码如下

json_encode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}

value
待编码的 value ,除了resource 类型之外,可感觉任何数据类型

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {“0”:{“0″:1,”1″:2,”2”:3}}

Normal: ["<foo>","'bar'",""baz"","&blong&","u00e9"] 
Tags: ["u003Cfoou003E","'bar'",""baz"","&blong&","u00e9"] 
Apos: ["<foo>","u0027baru0027",""baz"","&blong&","u00e9"] 
Quot: ["<foo>","'bar'","u0022bazu0022","&blong&","u00e9"] 
Amp: ["<foo>","'bar'",""baz"","u0026blongu0026","u00e9"] 
Unicode: ["<foo>","'bar'",""baz"","&blong&","é"] 
All: ["u003Cfoou003E","u0027baru0027","u0022bazu0022","u0026blongu0026","é"] 

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"}

$data='[{“Name”:”a1″,”Number”:”123″,”Contno”:”000″,”QQNo”:””},{“Name”:”a1″,”Number”:”123″,”Contno”:”000″,”QQNo”:””},{“Name”:”a1″,”Number”:”123″,”Contno”:”000″,”QQNo”:””}]’;
echo json_decode($data);

options
由以下常量组成的二进制掩码: JSON_HEX_QUOT, JSON_HEX_TAG,
JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT,
JSON_UNESCAPED_UNICODE.

以上例程会输出:

json_decode — 对 JSON 格式的字符串实行编码

 代码如下

Array ( [0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) )

 代码如下

Report a bug 参数

value
待编码的 value ,除了resource 类型之外,可感到此外数据类型

能够见到经过json_decode(卡塔尔国编写翻译出来的是目的,以往出口json_decode($data,true)试下

返回值
Returns an object or if the optional assoc parameter is TRUE, an
associative array is instead returned.

Example #3 接二连三与非三番五次数组示例

$b = array();

2.json_encode()

该函数只好接纳 UTF-8 编码的数目

<?php 
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; 
var_dump(json_decode($json)); 
var_dump(json_decode($json, true)); 
?>

<?php
$json = ‘{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>

Example #1 A json_encode() 的例子

非延续数组
array(4) {
[1]=>
string(3) “foo”
[2]=>
string(3) “bar”
[3]=>
string(3) “baz”
[4]=>
string(5) “blong”
}
string(43) “{“1″:”foo”,”2″:”bar”,”3″:”baz”,”4″:”blong”}”

Array ( [0] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) )

json_encode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

返回值
Returns an object or if the optional assoc parameter is TRUE, an
associative array is instead returned.

Example #1 json_decode() 的例子

json_encode — 对变量实行 JSON 编码

{“Name”:”a1″,
“Number”:”123″,
“Contno”:”000″
}

Report a bug 更新日志
版本 说明
5.4.0 options 参数扩充常量: JSON_PRETTY_PRINT,
JSON_UNESCAPED_SLASHES, 和 JSON_UNESCAPED_UNICODE。
5.3.3 options 参数扩展常量:JSON_NUMERIC_CHECK。
5.3.0 增加 options 参数.

echo json_encode($arr);
?>

说明
mixed json_decode ( string $json [, bool $assoc ] )
收受一个 JSON 格式的字符串而且把它转变为 PHP 变量

Example #2 json_encode(State of Qatar 函数中 options 参数的用法

json
待解码的 json string 格式的字符串。

<?php
$a = array(‘<foo>’,”‘bar'”,'”baz”‘,’&blong&’, “xc3xa9”);

echo “Non-associative array output as array: “, json_encode($c), “n”;
echo “Non-associative array output as object: “, json_encode($c,
JSON_FORCE_OBJECT), “nn”;

<?php
/* 处理json_encode汉语乱码
*/
$data = array (‘game’ => ‘冰火国度’, ‘name’ => ‘刺之灵’,
‘country’ =>
‘冰霜国’, ‘level’ => 45 );
echo json_encode ( $data );
echo “<br>”;
$newData = array ();
foreach ( $data as $key => $value ) {
$newData [$key] = urlencode ( $value );
}
echo urldecode ( json_encode ( $newData ) );
?>

 代码如下

以上例程会输出:

Array ( [0] => Array ( [Name] => a1 [Number] => 123
[Contno] => 000 [QQNo] => ) [1] => Array ( [Name] =>
a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2]
=> Array ( [Name] => a1 [Number] => 123 [Contno] =>
000 [QQNo] => ) )

echo “Associative array always output as object: “, json_encode($d),
“n”;
echo “Associative array always output as object: “, json_encode($d,
JSON_FORCE_OBJECT), “nn”;
?>

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), “nn”;

去除一个总是数组值的主意发出的非三番五次数组
array(3) {
[0]=>
string(3) “foo”
[2]=>
string(3) “baz”
[3]=>
string(5) “blong”
}
string(33) “{“0″:”foo”,”2″:”baz”,”3″:”blong”}”

结果:

说明
mixed json_decode ( string $json [, bool $assoc ] )
收受三个 JSON 格式的字符串而且把它调换为 PHP 变量

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

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

<?php
class myClass {
public $item1 = 1;
public $item2 = ‘中文’;
function to_json() {
//url编码,避免json_encode将汉语转为unicode
$this->item2 = urlencode($this->item2);
$str_json = json_encode($this);
//url解码,转完json后将各属性重回,确定保证目的属性不改变
$this->item2 = urldecode($this->item2);
return urldecode($str_json);
}
}
$c = new myClass();
echo json_encode($c);
echo ‘<br/>’;
echo $c->to_json();
echo ‘<br/>’;
echo json_encode($c);
echo ‘<br/>’;
echo json_encode(‘胥’);
?>

Report a bug 说明
string json_encode ( mixed $value [, int $options = 0 ] )
返回 value 值的 JSON 形式

Empty array output as array: []
Empty array output as object: {}

例如:’胥’经过json_encode管理后改为’u80e5’,最后的json中中文部分被轮换为unicode编码。大家要解决的正是将目的转变为json并确定保障对象内部的华语在json中仍然为以健康的国语现身,以后看来只使用json_encode是不可能落得指标的。
  作者的化解办法:先将类中的中文字段举行url编码(urlencode),然后再对目标开展json编码(jsonencode),最终url解码(urldecode)json,即最后的json,里面包车型客车国语仍为极度粤语!
测验代码如下:

assoc
当该参数为 TRUE 时,将回到
array 而非 object 。

结果为:

程序输出结果:

echo PHP_EOL.”删除三个接连数组值的秘技发生的非接二连三数组”.PHP_EOL;
unset($sequential[1]);
var_dump(
$sequential,
json_encode($sequential)
);
?>

 

echo PHP_EOL.”非一连数组”.PHP_EOL;
$nonsequential = array(1=>”foo”, 2=>”bar”, 3=>”baz”,
4=>”blong”);
var_dump(
$nonsequential,
json_encode($nonsequential)
);

<?php
$arr = array (‘a’=>1,’b’=>2,’c’=>3,’d’=>4,’e’=>5);

 代码如下

Report a bug 更新日志
版本 说明
5.4.0 options 参数扩展常量: JSON_PRETTY_PRINT,
JSON_UNESCAPED_SLASHES, 和 JSON_UNESCAPED_UNICODE。
5.3.3 options 参数扩张常量:JSON_NUMERIC_CHECK。
5.3.0 增加 options 参数.

options
由以下常量组成的二进制掩码: JSON_HEX_QUOT, JSON_HEX_TAG,
JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT,
JSON_UNESCAPED_UNICODE.

能够看到json_decode($data,trueState of Qatar输出的二个关周到组,因此可以知道json_decode($data)输出的是指标,而json_decode(“$arr”,true卡塔尔是把它免强生成PHP关联数组.

以上例程会输出:

 代码如下

json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode — 对 JSON 格式的字符串举行编码

json
待解码的 json string 格式的字符串。

2.json_encode()

object(stdClass)#1 (5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}

结果为:

Report a bug 返回值
编码成功则赶回叁个以 JSON 格局表示的 string 大概在曲折时回来 FALSE 。

上例将出口:

发表评论

电子邮件地址不会被公开。 必填项已用*标注