PHP使用JSON和將json還原成數(shù)組
在之前我寫過php返回json數(shù)據(jù)簡單實(shí)例,剛剛上網(wǎng),突然發(fā)現(xiàn)一篇文章,也是介紹json的,還挺詳細(xì),值得參考。內(nèi)容如下
從5.2版本開始,PHP原生提供json_encode()和json_decode()函數(shù),前者用于編碼,后者用于解碼。
一、json_encode()
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
輸出
{"a":1,"b":2,"c":3,"d":4,"e":5}
再看一個(gè)對象轉(zhuǎn)換的例子:
$obj->body = 'another post';
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo json_encode($obj);
輸出
[/code]
{
"body":"another post",
"id":21,
"approved":true,
"favorite_count":1,
"status":null
}
[/code]
由于json只接受utf-8編碼的字符,所以json_encode()的參數(shù)必須是utf-8編碼,否則會(huì)得到空字符或者null。當(dāng)中文使用GB2312編碼,或者外文使用ISO-8859-1編碼的時(shí)候,這一點(diǎn)要特別注意。
二、索引數(shù)組和關(guān)聯(lián)數(shù)組
PHP支持兩種數(shù)組,一種是只保存"值"(value)的索引數(shù)組(indexed array),另一種是保存"名值對"(name/value)的關(guān)聯(lián)數(shù)組(associative array)。
由于javascript不支持關(guān)聯(lián)數(shù)組,所以json_encode()只將索引數(shù)組(indexed array)轉(zhuǎn)為數(shù)組格式,而將關(guān)聯(lián)數(shù)組(associative array)轉(zhuǎn)為對象格式。
比如,現(xiàn)在有一個(gè)索引數(shù)組
$arr = Array('one', 'two', 'three');
echo json_encode($arr);
輸出
["one","two","three"]
如果將它改為關(guān)聯(lián)數(shù)組:
$arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
echo json_encode($arr);
輸出變?yōu)?/p>
{"1":"one","2":"two","3":"three"}
注意,數(shù)據(jù)格式從"[]"(數(shù)組)變成了"{}"(對象)。
如果你需要將"索引數(shù)組"強(qiáng)制轉(zhuǎn)化成"對象",可以這樣寫
json_encode( (object)$arr );
或者
json_encode ( $arr, JSON_FORCE_OBJECT );
三、類(class)的轉(zhuǎn)換
下面是一個(gè)PHP的類:
class Foo {
const ERROR_CODE = '404';
public $public_ex = 'this is public';
private $private_ex = 'this is private!';
protected $protected_ex = 'this should be protected';
public function getErrorCode() {
return self::ERROR_CODE;
}
}
現(xiàn)在,對這個(gè)類的實(shí)例進(jìn)行json轉(zhuǎn)換:
$foo = new Foo;
$foo_json = json_encode($foo);
echo $foo_json;
輸出結(jié)果是
{"public_ex":"this is public"}
可以看到,除了公開變量(public),其他東西(常量、私有變量、方法等等)都遺失了。
四、json_decode()
該函數(shù)用于將json文本轉(zhuǎn)換為相應(yīng)的PHP數(shù)據(jù)結(jié)構(gòu)。下面是一個(gè)例子:
$json = '{"foo": 12345}';
$obj = json_decode($json);
print $obj->{'foo'}; // 12345
通常情況下,json_decode()總是返回一個(gè)PHP對象,而不是數(shù)組。比如:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
結(jié)果就是生成一個(gè)PHP對象:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
如果想要強(qiáng)制生成PHP關(guān)聯(lián)數(shù)組,json_decode()需要加一個(gè)參數(shù)true:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json,true));
結(jié)果就生成了一個(gè)關(guān)聯(lián)數(shù)組:
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
五、json_decode()的常見錯(cuò)誤
下面三種json寫法都是錯(cuò)的,你能看出錯(cuò)在哪里嗎?
$bad_json = "{ 'bar': 'baz' }";
$bad_json = '{ bar: "baz" }';
$bad_json = '{ "bar": "baz", }';
對這三個(gè)字符串執(zhí)行json_decode()都將返回null,并且報(bào)錯(cuò)。
第一個(gè)的錯(cuò)誤是,json的分隔符(delimiter)只允許使用雙引號,不能使用單引號。第二個(gè)的錯(cuò)誤是,json名值對的"名"(冒號左邊的部分),任何情況下都必須使用雙引號。第三個(gè)的錯(cuò)誤是,最后一個(gè)值之后不能添加逗號(trailing comma)。
另外,json只能用來表示對象(object)和數(shù)組(array),如果對一個(gè)字符串或數(shù)值使用json_decode(),將會(huì)返回null。
var_dump(json_decode("Hello World")); //null
- php中JSON的使用與轉(zhuǎn)換
- PHP JSON 數(shù)據(jù)解析代碼
- php返回json數(shù)據(jù)函數(shù)實(shí)例
- php判斷是否為json格式的方法
- 教你如何使用PHP輸出中文JSON字符串
- PHP中讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法
- php處理json格式數(shù)據(jù)經(jīng)典案例總結(jié)
- 使用PHP接收POST數(shù)據(jù),解析json數(shù)據(jù)
- php json_encode與json_decode詳解及實(shí)例
- PHP生成及獲取JSON文件的方法
- php使用json-schema模塊實(shí)現(xiàn)json校驗(yàn)示例
相關(guān)文章
搭建PhpStorm+PhpStudy開發(fā)環(huán)境的超詳細(xì)教程
這篇文章主要介紹了搭建PhpStorm+PhpStudy開發(fā)環(huán)境的超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
PHP設(shè)計(jì)模式之?dāng)?shù)據(jù)訪問對象模式(DAO)原理與用法實(shí)例分析
這篇文章主要介紹了PHP設(shè)計(jì)模式之?dāng)?shù)據(jù)訪問對象模式(DAO)原理與用法,結(jié)合實(shí)例形式分析了PHP數(shù)據(jù)訪問對象模式的概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-12-12
php注冊審核重點(diǎn)解析(數(shù)據(jù)訪問)
這篇文章主要為大家解析了php注冊審核重點(diǎn),數(shù)據(jù)進(jìn)行訪問,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
PHP實(shí)現(xiàn)的帶超時(shí)功能get_headers函數(shù)
這篇文章主要介紹了PHP實(shí)現(xiàn)的帶超時(shí)功能的get_headers函數(shù),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-02-02

