LevelUp! Studio » json https://blog.levelup.in.th Experience the new world. Fri, 26 May 2017 10:06:07 +0000 th hourly 1 http://wordpress.org/?v=3.8.1 วิธีการทำลูป JSONClass ของ SimpleJSON [Unity C#] https://blog.levelup.in.th/2014/10/31/how-to-do-an-easy-for-loop-of-simplejson-jsonclass/ https://blog.levelup.in.th/2014/10/31/how-to-do-an-easy-for-loop-of-simplejson-jsonclass/#comments Fri, 31 Oct 2014 09:43:13 +0000 http://blog.levelup.in.th/?p=3883 หลังจากที่ได้รู้จักกับ JSONClass ใน Class SimpleJSON ซึ่งใช้ได้ค่อนข้างสะดวกกว่า Hashtable และ Dictionary มาก (แต่มีเงื่อนไขว่า Child ทั้งหมดเป็นได้แค่ String, Int, Float, Double, Boolean และตัวมันเอง) เนื่องจากสามารถเรียกค่าที่ซ้อนตัวมันเองได้ง่าย เช่น tempJsonClass["key"]["name"] ได้เลยซึ่งถ้าเก็บข้อมูลแบบนี้ไว้ใน Hashtable ล่ะก็ ต้องเรียก tempHashtable["key"] มาใส่ในอีก Hashtable ก่อนถึงจะเรียก key “name” ข้างในอีกทีออกมาได้ ซึ่งมีอย่างหนึงที่ SimpleJSON (จาก Link) ขาดไปคือการเรียก Keys เพื่อใช้ในการวนลูป (ซึ่งจรืงๆ SimpleJSON.cs ปกติมีฟังก์ชั่น GetEnumerator() ที่ Return Keys แต่ว่าเป็น IEnumerator ที่ต้องใช้ StartCouroutine()) เราเลยต้องเพิ่มโค๊ดส่วนหนึ่งเข้าไปใน Class SimpleJSON

เพิ่มโค๊ดด้านล่างนี้เข้าไปใน Class JSONClass ภายใน SimpleJSON.cs (Class อยู่ที่บรรทัด 696)

public Dictionary<string, JSONNode>.KeyCollection Keys{
  get { return m_Dict.Keys; }
}

แค่นี้เราก็สามารถเรียก Keys จาก JSONClass เพื่อมาใช้วน For Loop ได้แล้วล่ะครับ ตัวอย่างดังนี้

 

JSONClass tempJsonClass = SimpleJSON.JSON.Parse(result); //result เป็น String JSON ทั่วๆไป
foreach(string key in tempJsonClass .AsObject.Keys){ //ใช้ .AsObject.Keys เพื่อดึง Key ทั้งหมดได้เลย
  Debug.Log(key+":"+tempJsonClass[key]);
}

ง่ายๆเท่านี้แหละครับ

]]>
https://blog.levelup.in.th/2014/10/31/how-to-do-an-easy-for-loop-of-simplejson-jsonclass/feed/ 1
JSON Encode และ Decode ใน Flash Builder 4.6 https://blog.levelup.in.th/2012/04/30/json-encode-%e0%b9%81%e0%b8%a5%e0%b8%b0-decode-%e0%b9%83%e0%b8%99-flash-builder-46/ https://blog.levelup.in.th/2012/04/30/json-encode-%e0%b9%81%e0%b8%a5%e0%b8%b0-decode-%e0%b9%83%e0%b8%99-flash-builder-46/#comments Mon, 30 Apr 2012 04:58:44 +0000 http://blog.levelup.in.th/?p=1643 สืบเนื่องจาก บทความนี้ ผมต้องการที่จะส่งข้อมูลจาก php มายัง flex ในรูปแบบ JSON แต่ว่าในกระทู้นี้จำเป็นต้องหาไฟล์ corelib.swc หรือ as3corelib.swc แล้ว import เข้ามาช่วยทำการ encode และ decode ข้อมูลที่ส่งมาจากไฟล์ php แต่สำหรับ Flash Builder 4.6 ที่ผมใช้อยู่นั้น ผมก็ได้ลองทำตามกระทู้นี้ทุกขั้นตอน แต่ก็ยังเกิด error message ตามนี้

1120: Access of undefined property JSON.

Can not resolve a multiname reference unambiguously. JSON (from C:\Program Files\Adobe\Adobe Flash Builder 4.6\sdks\4.6.0\frameworks\libs\player\11.1\playerglobal.swc(JSON, Walker)) and com.adobe.serialization.json:JSON (from C:\Users\shippo\Downloads\corelib.swc(com.adobe.serialization.json:JSON)) are available.

ทำให้ทราบว่า class JSON มันดันซ้ำกันระหว่างไฟล์ playerglobal.swc (ซึ่งมีมากับ Flash Builder 4.6) และ corelib.swc มันจึงรันไม่ได้ซักที ก็พยายามเสิร์ชหาใน Google จนได้ความว่าใน Flash Builder 4.6 นั้น การที่จะ Encode หรือ Decode ข้อมูลที่มาในรูปแบบ JSON สามารถทำเลยโดย

1. การ decode JSON

var aData:* = JSON.parse(e.result as String);

จากตัวอย่างข้างต้นเป็นการ decode JSON ใน Flash Builder 4.6 แทนการใช้ JSON.decode
2. การ encode JSON

var objStr:String = JSON.stringify( {name:”Todd Anderson”, company:”Infrared5″, phone:15558576309}, deflate );

จากตัวอย่างข้างต้นเป็นการ encode JSON ใน Flash Builder 4.6 แทนการใช้ JSON.encode
ซึ่งทั้ง JSON.parse และ JSON.stringify เป็นเมธอดที่มีอยู่แล้วในไฟล์ playerglobal.swc หากต้องการเพียงแค่ encode และ decode ข้อมูลรูปแบบ JSON เราไม่จำเป็นต้องใช้ corelib.swc หรือ as3corelib.swc ก็ได้

]]>
https://blog.levelup.in.th/2012/04/30/json-encode-%e0%b9%81%e0%b8%a5%e0%b8%b0-decode-%e0%b9%83%e0%b8%99-flash-builder-46/feed/ 0
gzip ระหว่าง as3 + php https://blog.levelup.in.th/2009/11/30/gzip-in-as3-phpgzip-%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-as3-php/ https://blog.levelup.in.th/2009/11/30/gzip-in-as3-phpgzip-%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-as3-php/#comments Mon, 30 Nov 2009 08:14:13 +0000 http://blog.levelup.in.th/?p=279 อันนี้ก็มาจากการพยายามลดขนาดข้อมูลที่ต้องส่งไปมาระหว่าง Php กับ Flash ซึ่งเป็น Text ล้วนๆ ซึ่งก็ใหญ่ไม่ใช่เล่นเหมือนกัน

เลยพยายามลองใช้ gzip ที่มีติดอยู่กับ php เป็น default อยู่แล้ว

ประเด็นคือ gzip ของ php มี function เยอะมากมาย ประมาณ 3-4 แบบได้

ส่วน as3 ก็ไม่รู้ว่ามีหรือเปล่า ต้องหาอะไรเพิ่มมั้ย ก็เลยต้องศึกษาเพิ่มกันหน่อย

หลังจากวิจัยมาเกือบวัน ก็สรุปได้ผลตามนี้

PHP

หลังจากลองผิดลองถูกอยู่ซักพักก็ได้ โค้ดมาประมาณนี้

$log_zip = base64_encode(gzcompress(json_encode($log),5));

- แปลง Object เป็น Json ก่อนเพื่อความสะดวกในการใช้งาน

- gzip ด้วย gzcompress ระดับเท่าไหรก็แล้วแต่ เท่าที่ลองประมาณ 5 กำลังดี ทั้งเวลาและขนาด

- สุดท้ายเข้ารหัสเป็น Base64 เพื่อจะได้ส่งทาง Flashvar ได้

โดยส่งไปทาง FlashVars แบบนี้

<object classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″

width=”600″ height=”450″

codebase=”http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab” style=”visibility: visible;”>

<param name=”movie” value=”assets/flash/battle.swf” />

<param name=”quality” value=”high” />

<param name=”bgcolor” value=”#869ca7″ />

<param name=”wmode” value=”transparent” />

<param name=”allowScriptAccess” value=”sameDomain” />

<param name=”flashVars” value=”&log=<?=$log_zip?>” />

<embed src=”assets/flash/battle.swf” quality=”high” bgcolor=”#869ca7″ wmode=”transparent”

width=”600″ height=”450″ name=”flashBattle” align=”middle”

play=”true”

loop=”false”

quality=”high”battle_result

allowScriptAccess=”sameDomain”

flashVars=”&log=<?=$log_zip?>

type=”application/x-shockwave-flash”

pluginspage=”http://www.adobe.com/go/getflashplayer”>

</embed>

</object>

- ตัวแปล log_zip ส่งไปทาง FlashVars

AS3

หลังจากโง่งมหาอยู่นาน ปรากฏว่ามันมีอยู่แล้ว แต่ search แล้วดันเจอแต่ library เก่าๆ!

โค้ดก็ประมาณนี้หล่ะ เธอว์

var log_zip:String = root.loaderInfo.parameters.log;

var b64:Base64Decoder = new Base64Decoder();

log_zip = log_zip.replace(/ /g,”+”);

b64.decode(log_zip);

var ba:ByteArray = b64.flush();

ba.uncompress();

var log_text:String = ba.toString();

log = new JSONDecoder(log_text).getValue();

- ต้องมี Base64Decoder จาก as3lib ด้วย ไปหาโหลดเพิ่มเอา

- ที่เจ็บปวดคือส่งผ่าน FlashVar แล้วมันดันแปลง + เป็น space ให้ตามประสา urlencode! หาเป็นชั่วโมง!

- สุดท้ายก็แกะ Json ด้วย JSONDecoder (as3lib เหมือนกัน)

ผลจากการ gZip

ขนาดลดลงอย่างเห็นได้ชัดทีเดียว

เดิม = 8167 ตัวอักษร

gzip = 524 ตัวอักษร

ประหยัดไปเกือบๆ 20 เท่าทีเดียวเชียว

]]>
https://blog.levelup.in.th/2009/11/30/gzip-in-as3-phpgzip-%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-as3-php/feed/ 0