php 之 json格式
/*
JSON語法
數據在名稱/值對中
數據由逗號分隔
花括號保存對象
方括號保存數組
JSON 數據的書寫格式是:名稱/值對
名稱/值對包括字段名稱(在雙引號中),后面寫一個冒號,然后是值;如
"myweb":"lin3615"
等價于 myweb = "lin3615"
JSON 值,可以是數字,字符串(在雙引號中),邏輯值,數組{在方括號中},對象(在花括號中), null
*/
/*
mix json_decode($json [, true]) // 對$json 的格式數據解析, 為 true 時返回是數組,false 是對象
$aa = '{"a":"aa", "b": "bb"}';
print_r(json_decode($aa, true));
Array
(
[a] => aa
[b] => bb
)
$arr = json_decode($aa, true);
echo $arr['a']; // aa
echo $arr['b']; // bb
$obj = json_decode($aa);
echo "<br />";
echo $obj->a; // aa
echo "<br />";
echo $obj->b; // bb
*/
// string json_encode(mix $value) 對 $value 進行 json 格式編碼
/*
$arr = array('a' => 'l', 'b' => 'i', 'c' => 'n');
$str = json_encode($arr);
echo $str; // {"a":"l","b":"i","c":"n"}
*/
/*
$str = array('l', 'i', 'n');
$strs = json_encode($str);
echo $strs; // ["l","i","n"]
*/
// 對象換成了 json 格式的字符,可以保存入數據庫等
class test
{
public $a = 'lin3615';
public $c = 'hi, world';
private $d = 'private';
public function __construct()
{
return '__contruct';
}
public function ok()
{
return 'hi';
}
}
$obj = new test();
$oo = json_encode($obj); // {"a":"lin3615","c":"hi, world"}
echo $oo; echo '<br />';
$str = json_encode($obj->a);
echo $str; // "lin3615"
$str2 = json_encode($obj->ok());
echo $str2; // "hi"
總結
以上是生活随笔為你收集整理的php 之 json格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: excel SUBTOTAL函数使用详解
- 下一篇: 浅谈Kotlin(七):lateinit