PHP 中操作JSON的一些笔记。
简介
JSON(JavaScript Object Notation)最初是javascript对象的一种格式,后来越来越多的的高级语言也支持这种格式,PHP也不例外。
php数据转化成JSON
其实非常简单,只要json_encode()函数就可以实现,具体代码如下
1、php数组转化为Json
1 2 3 4 5 6 7
| $concerts_array = array( array("product" => "book", "price" => 23.1, "num" => 3), array("product" => "food", "price" => 32.87, "num" => 2), array("product" => "computer", "price" => 6534, "num" => 1) );
echo json_encode($concerts_array);
|
然后在浏览器里显示如下
1
| [{"product":"book","price":23.1,"num":3},{"product":"food","price":32.87,"num":2},{"product":"computer","price":6534,"num":1}]
|
2、php对象转化为Json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $concerts_class = new stdclass();
$concerts_class->book = new stdclass(); $concerts_class->book->price = 23.1; $concerts_class->book->num = 3;
$concerts_class->food = new stdclass(); $concerts_class->food->price = 32.87; $concerts_class->food->num = 2;
$concerts_class->computer = new stdclass(); $concerts_class->computer->price = 6534; $concerts_class->computer->num = 1;
echo json_encode($concerts_class);
|
首先看下$concerts_class这个对象的结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| object(stdClass) ["book"]=> object(stdClass) ["price"]=> float(23.1) ["num"]=> int(3) } ["food"]=> object(stdClass) ["price"]=> float(32.87) ["num"]=> int(2) } ["computer"]=> object(stdClass) ["price"]=> int(6534) ["num"]=> int(1) } }
|
转化后的Json数据为
1
| {"book":{"price":23.1,"num":3},"food":{"price":32.87,"num":2},"computer":{"price":6534,"num":1}}
|
Json格式数据转化成php可操作数据类型
这一步也是一个json_decode()函数就可以搞定的事情,其中第二的参数是一个开关,默认为false,可就是默认转化为object,当传入true时,就是转化为array类型。
1 2 3 4 5 6 7 8 9 10 11 12 13
| $json_array = '[{"product":"book","price":23.1,"num":3},{"product":"food","price":32.87,"num":2},{"product":"computer","price":6534,"num":1}]'; $content_class = json_decode($json_array, true); $content_array = json_decode($json_array, false); var_dump($content_array); var_dump($content_class);
echo "\n===============================(看毛看,没看过昏割线嘛)=======================================\n";
$json_class = '{"book":{"price":23.1,"num":3},"food":{"price":32.87,"num":2},"computer":{"price":6534,"num":1}}'; $content_class = json_decode($json_class, true); $content_array = json_decode($json_class, false); var_dump($content_array); var_dump($content_class);
|
然后浏览器里显示对应如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| array(3) { [0]=> object(stdClass) ["product"]=> string(4) "book" ["price"]=> float(23.1) ["num"]=> int(3) } [1]=> object(stdClass) ["product"]=> string(4) "food" ["price"]=> float(32.87) ["num"]=> int(2) } [2]=> object(stdClass) ["product"]=> string(8) "computer" ["price"]=> int(6534) ["num"]=> int(1) } }
array(3) { [0]=> array(3) { ["product"]=> string(4) "book" ["price"]=> float(23.1) ["num"]=> int(3) } [1]=> array(3) { ["product"]=> string(4) "food" ["price"]=> float(32.87) ["num"]=> int(2) } [2]=> array(3) { ["product"]=> string(8) "computer" ["price"]=> int(6534) ["num"]=> int(1) } }
===============================(看毛看,没看过昏割线嘛)======================================= object(stdClass) ["book"]=> object(stdClass) ["price"]=> float(23.1) ["num"]=> int(3) } ["food"]=> object(stdClass) ["price"]=> float(32.87) ["num"]=> int(2) } ["computer"]=> object(stdClass) ["price"]=> int(6534) ["num"]=> int(1) } }
array(3) { ["book"]=> array(2) { ["price"]=> float(23.1) ["num"]=> int(3) } ["food"]=> array(2) { ["price"]=> float(32.87) ["num"]=> int(2) } ["computer"]=> array(2) { ["price"]=> int(6534) ["num"]=> int(1) } }
|
The End~