日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php中json_decode返回数组或对象的实例

發布時間:2023/12/10 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php中json_decode返回数组或对象的实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.json_decode()

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

json_decode — 對 JSON 格式的字符串進行編碼

說明
mixed json_decode ( string $json [, bool $assoc ] )
接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量

參數

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

assoc
當該參數為 TRUE 時,將返回 array 而非 object 。


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

范例

Example #1 json_decode() 的例子

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

上例將輸出:

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)
}http://www.111cn.net/phper/php-cy/57800.htm


$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);

結果為:

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_decode()編譯出來的是對象,現在輸出json_decode($data,true)試下

?代碼如下 復制代碼
echo json_decode($data,true);

結果:

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] => ) )
?

可以看出 json_decode($data,true)輸出的一個關聯數組,由此可知json_decode($data)輸出的是對象,而json_decode("$arr",true)是把它強制生成PHP關聯數組.


假如我們獲取的JSON數據如下:(可以使用curl、fsockopen等方式獲取)

?代碼如下 復制代碼
{
?"from":"zh",
?"to":"en",
?"trans_result":[
? {
?? "src":"u4f60u597d",
?? "dst":"Hello"
? }
?]
}
?

一、json_decode返回array的方式:

json_decode($data,true);用json_decode函數返回array的方式得到:

?代碼如下 復制代碼
Array
(
??? [from] => zh
??? [to] => en
??? [trans_result] => Array
??????? (
??????????? [0] => Array
??????????????? (
??????????????????? [src] => 你好
??????????????????? [dst] => Hello
??????????????? )

??????? )

)
?

我們在PHP語言中可以用以下方法取得我們想要的值:

?代碼如下 復制代碼
<?php
$data = <<<STR
{
?"from":"zh",
?"to":"en",
?"trans_result":[
? {
?? "src":"u4f60u597d",
?? "dst":"Hello"
? }
?]
}
STR;
$jsondata=json_decode($data,true);
header("Content-Type: text/html; charset=UTF-8");
print_r($jsondata);www.111cn.net
echo "<br />".$jsondata['to']; //en
echo "<br />".$jsondata['trans_result'][0]['dst']; //Hello
?>
?

二、json_decode返回object的方式:

json_decode($data);

用json_decode函數返回object的方式得到:

?代碼如下 復制代碼
stdClass Object
(
??? [from] => zh
??? [to] => en
??? [trans_result] => Array
??????? (
??????????? [0] => stdClass Object
??????????????? (
??????????????????? [src] => 你好
??????????????????? [dst] => Hello
??????????????? )

??????? )

)
?

我們在PHP語言中可以用以下方法取得我們想要的值:

?代碼如下 復制代碼
<?php
$data = <<<STR
{
?"from":"zh",
?"to":"en",
?"trans_result":[
? {
?? "src":"u4f60u597d",
?? "dst":"Hello"
? }
?]
}

STR;
$jsondata=json_decode($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($jsondata);
echo "<br />".$jsondata->from; //zh
echo "<br />".$jsondata->trans_result[0]->src; //你好
?>
?

更多詳細內容請查看:http://www.111cn.net/phper/php-cy/57800.htm

轉載于:https://www.cnblogs.com/alibai/p/3547485.html

總結

以上是生活随笔為你收集整理的php中json_decode返回数组或对象的实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。