Flash Player 11.0 / AIR 3.0 開始支援原生的 JSON
屬預設套件 (default package) 的 JSON 類別, 方法同 JavaScript 的用法, 靜態方法使用 stringify() 和 parse()。注意: 在此並不是用 encode() 和 decode()。
2012-03-30
2012-03-18
AS-PHP-talk 簡介
AS-PHP-talk 放到 github 後, 不知不覺已經過了一年 =..=, 突然想來寫個介紹。
這個專案的目的是為了讓 ActionScript 和 PHP 溝通有另一種較為簡單的選擇, 目前後端只有 PHP 的實作。 以往後端 ( 筆者較常使用 PHP ) 和 AS 溝通, 比較舊的方式是通過 XML, 比較多人用的是透過 AMF ( Flash remoting ), AS-PHP-talk 則是使用 JSON。
JSON 和 XML 相比的話, 同樣都是文字格式。XML 可以制訂的資料比較複雜; 然而, JSON 可以描述 Object、Array 和基本類型的資料 , 其實已經很夠用。
JSON 和 AMF 相比的話, AMF 體積應該會比較小, 但由於 AMF 是二元資料, 除錯相對於文字格式的 JSON 要來得麻煩。JSON 資料可以直接秀在網頁上, 有錯誤時也可以在網頁上就查覺。
前端和後端資料交換的程式, 最麻煩就是除錯。AS-PHP-talk的目的是, 可以在完全沒撰寫前端程式 ( Flash ) 之前, 利用簡單的網頁就達到後端 PHP 程式的除錯。後端的開發人員可以完全不用理會前端程式的開發進程; 前端的開發人員也可以透過簡單的後端 ( 送假資料 ) , 進行前端的開發。前、後端的程式可以很方便的切開來。
這個專案的目的是為了讓 ActionScript 和 PHP 溝通有另一種較為簡單的選擇, 目前後端只有 PHP 的實作。 以往後端 ( 筆者較常使用 PHP ) 和 AS 溝通, 比較舊的方式是通過 XML, 比較多人用的是透過 AMF ( Flash remoting ), AS-PHP-talk 則是使用 JSON。
JSON 和 XML 相比的話, 同樣都是文字格式。XML 可以制訂的資料比較複雜; 然而, JSON 可以描述 Object、Array 和基本類型的資料 , 其實已經很夠用。
JSON 和 AMF 相比的話, AMF 體積應該會比較小, 但由於 AMF 是二元資料, 除錯相對於文字格式的 JSON 要來得麻煩。JSON 資料可以直接秀在網頁上, 有錯誤時也可以在網頁上就查覺。
前端和後端資料交換的程式, 最麻煩就是除錯。AS-PHP-talk的目的是, 可以在完全沒撰寫前端程式 ( Flash ) 之前, 利用簡單的網頁就達到後端 PHP 程式的除錯。後端的開發人員可以完全不用理會前端程式的開發進程; 前端的開發人員也可以透過簡單的後端 ( 送假資料 ) , 進行前端的開發。前、後端的程式可以很方便的切開來。
2011-10-17
jsonTrace 小工具
QopDump 好像有不少人使用,但感覺還是不理想,於是利用 com.adobe.serialization.json package 和 JsonFormater 組合起來,弄了個 jsonTrace() 。
JSONTrace 專案位置在 https://github.com/shinder/JSONTrace
標頭定義為:
public function jsonTrace(rawObj:Object=null, noTrace:Boolean=false, noFormat:Boolean=false):String
參數:
JSONTrace 專案位置在 https://github.com/shinder/JSONTrace
標頭定義為:
public function jsonTrace(rawObj:Object=null, noTrace:Boolean=false, noFormat:Boolean=false):String
參數:
- rawObj : 所欲查看的物件 (主要類型為 Object 和 Array)
- noTrace : true 表示不 trace 輸出, 只回傳結果字串
- noFormat : true 表示不做 JSON 字串的對位格式
2010-08-12
從 AS3 改寫的 JavaScript JSON Formatter
參考 http://qops.blogspot.com/2010/04/json-formatter.html
用法
用法
var jf = new JsonFormatter(JSON.stringify(response));
jQuery("#show1").html("<pre>" + jf.getResult() + "</pre>");2010-08-11
目前瀏覽器幾乎都實作了 JavaScript 的 JSON 物件
以往在 JavaScript 要使用 JSON 的功能必須要使用 http://www.json.org/json2.js。
目前各家 browser 應該都實作了 JSON 功能,無需使用 JavaScript 的 library,
直接就可以使用 JSON.stringify() 和 JSON.parse()。
測試過的有 FireFox 3.6、Google Chrome、IE8。手持裝置的瀏覽器沒試過。
目前各家 browser 應該都實作了 JSON 功能,無需使用 JavaScript 的 library,
直接就可以使用 JSON.stringify() 和 JSON.parse()。
測試過的有 FireFox 3.6、Google Chrome、IE8。手持裝置的瀏覽器沒試過。
2010-06-15
Groovy 使用 JSON-simple
想在 Groovy 上使用 JSON,想說 Json-lib 還滿多人用的,就試試看。結果,Json-lib 相依性太多,還需要另外 4 支 jar,而且 run 的時候,錯誤一堆,浪費一些時間後,決定放棄 Json-lib。
後來找到 json-simpe, 果然比較簡單, 搭配 Groovy 顯得更容易使用。不過中文的部份似乎不會轉成 Unicode 的表示法, 這部份在傳給別的程式語言時可能會發生問題。
後來找到 json-simpe, 果然比較簡單, 搭配 Groovy 顯得更容易使用。不過中文的部份似乎不會轉成 Unicode 的表示法, 這部份在傳給別的程式語言時可能會發生問題。
2010-04-09
JSON Formatter 用法
Frame Actions:
import lin.shinder.utils.JsonFormatter;
input_txt.text='[{"name":"shinder", "num":"123"}, {"name":"dephne", "num":"124"}]';
format_btn.addEventListener(MouseEvent.CLICK, formatClick);
function formatClick(event) {
var jf:JsonFormatter=new JsonFormatter(input_txt.text);
output_txt.text = jf.result;
}
2009-12-24
Flash Media Server 加上 JSON 功能
1) 到 http://www.json.org/ 找 ActionScript 1 的版本
2) 修改其中 JSON.error 的部份, 存成 JSON.asc
3) 將檔案放到 /scriptlib 或 application目錄
4) 在 main.asc 裡用「load( "JSON.asc" );」
修改後的 code:
2) 修改其中 JSON.error 的部份, 存成 JSON.asc
3) 將檔案放到 /scriptlib 或 application目錄
4) 在 main.asc 裡用「load( "JSON.asc" );」
修改後的 code:
/*
Copyright (c) 2005 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
Ported to Actionscript May 2005 by Trannie Carter ,
wwww.designvox.com
Ported to Actionscript 1 May 2006 by Malte Ubl ,
http://joose-js.blogspot.com
2006-11-18: Fixed assumption that an empty string evaluates to false in boolean
context that is wrong in Flash 8 an later.
2007-04-08: Fixed bad behavior with switch statements when publishing to Flash 6 from Flash MX.
Report and Patch by Dan Wich
Updated 2007-04-08
USAGE:
var o = JSON.parse(jsonStr);
var s = JSON.stringify(obj);
// If there is an error, the method calls _root.debug("Error message...")
// Override JSON.error for the desired behaviour
*/
JSON = new Object();
// shinder.lin modified 2009-12-24
JSON.error = function(msg) {
JSON.error_occured = true;
trace(msg);
}
JSON.stringify = function (arg) {
var c, i, l, s = '', v;
JSON.error_occured = false;
switch (typeof arg) {
case 'object':
if (arg) {
if (arg instanceof Array) {
for (i = 0; i < arg.length; ++i) {
v = JSON.stringify(arg[i]);
if (s != '') {
s += ',';
}
s += v;
}
return '[' + s + ']';
} else if (typeof arg.toString != 'undefined') {
for (i in arg) {
v = arg[i];
if (typeof v != 'undefined' && typeof v != 'function') {
v = JSON.stringify(v);
if (s != '') {
s += ',';
}
s += JSON.stringify(i) + ':' + v;
}
}
return '{' + s + '}';
}
}
return 'null';
case 'number':
return isFinite(arg) ? String(arg) : 'null';
case 'string':
l = arg.length;
s = '"';
for (i = 0; i < l; i += 1) {
c = arg.charAt(i);
if (c >= ' ') {
if (c == '\\' || c == '"') {
s += '\\';
}
s += c;
} else {
switch (c) {
case '\b':
s += '\\b';
break;
case '\f':
s += '\\f';
break;
case '\n':
s += '\\n';
break;
case '\r':
s += '\\r';
break;
case '\t':
s += '\\t';
break;
default:
c = c.charCodeAt();
s += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return s + '"';
case 'boolean':
return String(arg);
default:
return 'null';
}
}
JSON.parse = function (text) {
var at = 0;
var ch = ' ';
JSON.error_occured = false;
function error(m) {
JSON.error("JSONError: "+m)
}
function next() {
ch = text.charAt(at);
at += 1;
return ch;
}
function white() {
while (!JSON.error_occured && ch != null) {
if (ch <= ' ') {
next();
} else if (ch == '/') {
next();
switch (ch) {
case '/':
while (!JSON.error_occured && next() != null && ch != '\n' && ch != '\r') {}
break;
case '*':
next();
while (true) {
if (ch) {
if (ch == '*') {
if (next() == '/') {
next();
break;
}
} else {
next();
}
} else {
error("Unterminated comment");
}
}
break;
default:
error("Syntax error");
}
} else {
break;
}
}
}
function str() {
var i, s = '', t, u;
var outer = false;
if (ch == '"') {
while (!JSON.error_occured && next() != null) {
if (ch == '"') {
next();
return s;
} else if (ch == '\\') {
next();
switch (ch) {
case 'b':
s += '\b';
break;
case 'f':
s += '\f';
break;
case 'n':
s += '\n';
break;
case 'r':
s += '\r';
break;
case 't':
s += '\t';
break;
case 'u':
u = 0;
for (i = 0; i < 4; i += 1) {
t = parseInt(next(), 16);
if (!isFinite(t)) {
outer = true;
break;
}
u = u * 16 + t;
}
if(outer) {
outer = false;
break;
}
s += String.fromCharCode(u);
break;
default:
s += ch;
}
} else {
s += ch;
}
}
}
error("Bad string");
}
function arr() {
var a = [];
if (ch == '[') {
next();
white();
if (ch == ']') {
next();
return a;
}
while (!JSON.error_occured && ch != null) {
a.push(value());
white();
if (ch == ']') {
next();
return a;
} else if (ch != ',') {
break;
}
next();
white();
}
}
error("Bad array");
}
function obj() {
var k, o = {};
if (ch == '{') {
next();
white();
if (ch == '}') {
next();
return o;
}
while (!JSON.error_occured && ch != null) {
k = str();
white();
if (ch != ':') {
break;
}
next();
o[k] = value();
white();
if (ch == '}') {
next();
return o;
} else if (ch != ',') {
break;
}
next();
white();
}
}
error("Bad object");
}
function num() {
var n = '', v;
if (ch == '-') {
n = '-';
next();
}
while (!JSON.error_occured && ch >= '0' && ch <= '9') {
n += ch;
next();
}
if (ch == '.') {
n += '.';
next();
while (!JSON.error_occured && ch >= '0' && ch <= '9') {
n += ch;
next();
}
}
if (ch == 'e' | ch == 'E') {
n += ch;
next();
if (ch == '-' || ch == '+') {
n += ch;
next();
}
while (!JSON.error_occured && ch >= '0' && ch <= '9') {
n += ch;
next();
}
}
v = Number(n);
if (!isFinite(v)) {
error("Bad number");
}
return v;
}
function word() {
switch (ch) {
case 't':
if (next() == 'r' && next() == 'u' &&
next() == 'e') {
next();
return true;
}
break;
case 'f':
if (next() == 'a' && next() == 'l' &&
next() == 's' && next() == 'e') {
next();
return false;
}
break;
case 'n':
if (next() == 'u' && next() == 'l' &&
next() == 'l') {
next();
return null;
}
break;
}
error("Syntax error");
}
function value() {
white();
switch (ch) {
case '{':
return obj();
case '[':
return arr();
case '"':
return str();
case '-':
return num();
default:
return ch >= '0' && ch <= '9' ? num() : word();
}
}
return value();
}
2008-06-04
PHP 使用 JSON 和 AS3 溝通
JSON 常用在 AJAX 的資料交換上,其實也可以用在 SWF<->PHP 上。
PHP5有內建 JSON 的函式;PHP4沒有,但可以用 PEAR 的 JSON 類別。
AS3 則可以用 as3corelib 裡的 JSON 類別。
以下是將DB資料轉換成JSON的例子。
mysql2json4.php
test_mysql2json4.php
test_json.fla frame actions:
資料傳給 PHP
test_json_insert4.php
test_json_submit.fla frame actions:
相關檔案下載
PHP5有內建 JSON 的函式;PHP4沒有,但可以用 PEAR 的 JSON 類別。
AS3 則可以用 as3corelib 裡的 JSON 類別。
以下是將DB資料轉換成JSON的例子。
mysql2json4.php
/*** shinder.lin@gmail.com
for PHP4
*/
require_once("JSON.php");
function mysql2json4($sql) {
if(is_string($sql)) {
$query = mysql_query($query) or die(mysql_error());
} else {
$query = $sql;
}
$ar = array();
if($total = mysql_num_rows($query)) {
while($row = mysql_fetch_assoc($query)) {
$obj = array();
foreach($row as $key => $value) {
$obj[ $key ] = $value;
}
array_push($ar, $obj);
}
mysql_data_seek($query, 0);
}
$json = new Services_JSON();
return $json->encode($ar);
}
?>
test_mysql2json4.php
require("mysql2json4.php");
mysql_connect("localhost", "root", "");
mysql_select_db("test");
mysql_query("SET NAMES 'utf8'");
$q = mysql_query("select * from ctest");
echo mysql2json4($q);
?>test_json.fla frame actions:
import com.adobe.serialization.json.*;
var ur:URLRequest = new URLRequest("http://localhost/json_test/test_mysql2json4.php");
var ul:URLLoader = new URLLoader();
ul.addEventListener(Event.COMPLETE, onComplete);
ul.load(ur);
function onComplete(evt:Event) {
trace(ul.data);
var o:Object = JSON.decode(ul.data);
QopDump.echo(o);
}
資料傳給 PHP
test_json_insert4.php
require_once("JSON.php");
$raw = $GLOBALS["HTTP_RAW_POST_DATA"];
$json = new Services_JSON();
$ar = (array) $json->decode($raw);
mysql_connect("localhost", "root", "");
mysql_select_db("test");
mysql_query("SET NAMES 'utf8'");
// watch out for sql injection
$sql = sprintf("INSERT INTO `ctest` (`sno`, `name`, `phone`) VALUES (NULL, '%s', '%s')", $ar['name'], $ar['phone']);
echo (bool) mysql_query($sql);
?>test_json_submit.fla frame actions:
import com.adobe.serialization.json.*;
var ur:URLRequest = new URLRequest("http://localhost/json_test/test_json_insert4.php");
ur.method = URLRequestMethod.POST;
var ul:URLLoader = new URLLoader;
submit_btn.addEventListener(MouseEvent.CLICK, onSubmit);
function onSubmit(evt:MouseEvent):void {
var obj:Object = {name:name_ti.text, phone:phone_ti.text};
var str:String = JSON.encode(obj);
ur.data = str;
ur.contentType = "text/plain";
ul.load(ur);
// trace(str);
}
ul.addEventListener(Event.COMPLETE, onComplete);
function onComplete(evt:Event) {
info_ta.text = ul.data;
}
相關檔案下載
訂閱:
文章 (Atom)