建立Json字串Example

Download Report

Transcript 建立Json字串Example

吃遊網
9862301 陳致霖
9862216 鄭雅如
OUTLINE
Search
Blog
Shop
 Hot/New
User
 Reply
 Message
 Friends
 Favorite
Album
 Password
SEARCH
站內搜尋
 運用php Ajax Json

WHAT IS JSON
JavaScript Object Notation的縮寫
 以純文字為基底去儲存和傳送簡單結構資料
 優點

相容性高
 格式容易瞭解,閱讀及修改方便
 支援許多資料格式(number, string, boolean, null,
array, associative array)
 許多程式都支援函式庫讀取或修改 JSON 資料

JSON的應用
建立JSON字串
可包含Array data 或 Object data
 Array用[ ]來寫入資料
 Object用{ }來寫入資料
 Name / Value 是成對的,中間透過 ”:” 來區隔

建立JSON字串 EXAMPLE
var cart = {
"orderID": 12345,
"shopperName": "John Smith",
"contents": [
{
"productID": 34,
"productName": "SuperWidget",
"quantity": 1
},
],
"orderCompleted": true
};
利用 PHP 建立 JSON 資料
PHP 內建函式庫json_encode 跟 json_decode
 用 array 方式將資料輸出,再透過 json_encode

<?php
$cart = array(content);
echo json_encode( $cart );
?>
利用 PHP 讀取 JSON 資料
PHP 內建函式庫 json_decode
 利用了 json_decode 將 json 轉成變數資料

<?php
$jsonString = ‘content’;
$cart = json_decode( $jsonString );
echo $cart->shopperEmail . "<br>";
echo $cart->contents[1]->productName . "<br>";
?>
SHOP
鉅細靡遺:店家詳細資訊
 衛星定位:Google Map

USER
廣結善緣:將其他使用者加為好友
 魚雁往返:站內信
 各有所好:收藏列表

BLOG
首頁訊息:Hot/New
 我的首頁:發表新文章
 圖文並茂:能插入圖片
 意見交流:網友回覆
 使用工具:CKEditor

CKEDITOR
Open Source
 網頁編輯器
 見即所得
 Base on Javascript




支援各大瀏覽器 Ex: IE, Firefox, Chrome…
支援各種sever技術 Ex: ASP, PHP…
介面
ALBUM

個人隱私:密碼保護