|
 |
|
อยากจะถามพี่ num เพื่อผมจะได้ออกจากกะลา ซักที เกริ่นนำครับ คือปรกติที่ทุกวันนี้ ผมใช้ ผมใช้ ajax ส่งแต่ html, string, ค่า ค่าเดียว เท่านั้น |
|
 |
|
|
 |
 |
|
เจสันก็เป็นพระเอกหลังระดับดาราฮอลลีวู๊ตเลยนะเพ่เอี่ยว ไม่รู้จักได้ไง
อิอิอิ
ล้อเล่งน่าาาา
|
 |
 |
 |
 |
Date :
2009-10-07 11:47:28 |
By :
NanoThoro |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทีแรกจะรอให้พี่ Num เค้ามาตอบซะก่อน
ในเมื่อคุณ นาโน เปิดก่อน ผมก็เนียลตามน้ำครับ 
ปล.อยากรู้เหมือนกับคุณเอี่ยว พี่ Num มาตอบเร็วๆครับ 
|
 |
 |
 |
 |
Date :
2009-10-07 11:57:18 |
By :
panyapol |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมพอรุ้บ้างนิดหน่อย แต่เพื่อความกระจ่าง อยากรอพี่หนุ่มดเหมือนกันครับ
ใน php มี function เตรียมไว้คือ json_encode กับ json_decode
มีประโยชน์ในการ แปลงตัวแปร เป็นข้อความแล้วเก็บไว้ใน text ในกรณีไม่อยากใช้ database และอื่นๆ อีกมากมายเลย
|
 |
 |
 |
 |
Date :
2009-10-07 12:02:35 |
By :
danya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คนสำคัญรอความหวัง
|
 |
 |
 |
 |
Date :
2009-10-07 12:06:11 |
By :
gummezaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
555 รอก่อนจ้า กำลังเรียบเรียงอยู่ สงสัยจานาน 
|
 |
 |
 |
 |
Date :
2009-10-07 12:10:14 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
รอด้วยความหวัง
|
 |
 |
 |
 |
Date :
2009-10-07 12:11:45 |
By :
NanoThoro |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สัก 2 ปีค่อยตอบก้อม่ายสาย อิอิ
|
 |
 |
 |
 |
Date :
2009-10-07 12:13:56 |
By :
gummezaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เต็มที่เลยคับ แน่นๆก็ดี เผื่อคนที่ไม่เคยได้ยินจะได้เข้าใจด้วยคับ 
|
 |
 |
 |
 |
Date :
2009-10-07 12:16:59 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จ้าเท่าที่รู้นะ :D
1)
json เอาไว้ใช้แปลงข้อมูลไปมาระหว่างภาษาน่ะ
มันเป็นข้อมูลที่อยู่ในรูปแบบ javascript object notation ซึ่งในภาษาต่างๆ สร้างคำสั่งสำหรับแปลง json
ให้อยู่ในรูปแบบโครงสร้างข้อมูลในภาษานั้นๆได้โดยตรง เลยอย่างเช่น array, object, string ฯลฯ
แต่ถ้า xml จะเป็น tag ธรรมดาๆ ซึ่งไม่มีความเกี่ยวข้องกับโครงสร้างข้อมูลในภาษาต่างๆ อย่าง array หรือ object เลย
เวลาใช้ก็เลยต้องไล่ parse tag xml จากนั้นค่อยนำมาแปลงกลับเป็น array หรือ object
ซึ่งถ้าไม่นับรวมการใช้ xml มาใช้ในการจัดเก็บข้อมูลซึ่ง xml มีโครงสร้างเหมือนฐานข้อมูลมากกว่าทำให้ง่ายในการ search
ในการแลกเปลี่ยนข้อมูลระหว่างภาษา อย่างเช่น javascript และ php, xml จะใช้ยากกว่า json ครับ
เรื่องนิยามและรายละเอียดต่างๆ อ่านที่ http://json.org/ ละกันครับ :)
เนื่องจากมันเป็น string ธรรมดาๆ ทำให้สามารถนำตัวแปร แปลงเป็น json แล้วเก็บใน text file ได้เลยง่ายต่อการใช้อย่างมากเช่น
file_put_contents('test.txt',json_encode(array('test','test2')));
เวลาจะเอาตัวแปรมาใช้ก็
$array = json_decode(file_get_contents('test.txt'));
ซึ่งจะดีกว่า serailize() ของ php ตรงที่ test.txt นี่เป็นข้อมูลที่สามารถแปลงเป็นโครงสร้างข้อมูลในภาษาอื่นๆ ได้เกือบทุกภาษา :D
2)
ใน json ถึงแม้ว่าพอจะทำให้เป็น ansi หรือ tis620 ได้แต่ก็ไม่นิยมทำกันจะทำให้เป็น utf8 มากกว่าครับ
ดังนั้นในการแปลงข้อมูลภาษาไทยจะต้องแปลงให้เป็น utf8 ก่อนครับ
แง่บๆ พูดถึงในการแปลงข้อมูลใน php เป็น json นะแต่ใน javascript รูปแบบการจัดเก็บไม่รู้เป็นเป็น utf8 utf16 หรืออะไรก็ไม่รู้
echo json_encode(array('ภาษาไทย','test2'));
เวลา save ไฟล์โค้ด php นี้จะต้องเซฟเป็น utf8 ครับ
แต่ถ้าเซฟไฟล์ php นี้เป็น ansi ก็จะต้องแปลงข้อมูลก่อนเช่น echo json_encode(array(iconv('TIS-620','UTF-8','ภาษาไทย'),'test2'));
3)
ในการใช้กับ ajax ก็ควรจะระบุ header เป็น utf8 ด้วยครับ :D
<?php
header('Content-type:application/json;charset=utf-8');
echo json_encode(array('ภาษาไทย','test2'));
สำหรับฝั่ง javascript สมมุติได้รับข้อมูลมาจากการ request ajax แล้วก็ให้ทำการ eval ตัวแปรครับ
eval('var data = '+responseText+';');
ก็จะได้ข้อมูลมาใช้งานครับ alert(data[0]+','+data[1]);
พอดี javascript , json เป็นโครงสร้างข้อมูลของภาษาโดยตรงเลยใช้ eval ได้เลยครับ
ถ้าเป็นภาษาอื่นๆ จะมี function decode json string อีกทีหนึ่งครับ
ถึงแม้ว่าตอนเริ่มต้นข้อมูลของเราจะเป็น utf8 แต่เราสามารถใช้ข้อมูลที่ได้รับจาก php
ในไฟล์ html ที่เป็น tis620 หรือ windows874 ได้ครับ เนื่องจากการข้อมูลที่ได้รับเมื่อมาอยู่ในรูป javascript
จะถูก browser แปลง encoding ให้ตาม encoding ของหน้านั้นโดยอัตโนมัติครับ
ตัวอย่างเช่นหน้าที่ระบุ charset โดยใช้
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
ก็ใช้ข้อมูลจาก
<?php
header('Content-type:application/json;charset=utf-8');
echo json_encode(array('ภาษาไทย','test2'));
ได้เลย โดยไม่ต้องแปลง encoding ครับ
4) serialize() ใน jquery จะดึงเอาข้อมูลใน form ให้อยู่ในรูปแบบ input1=1&input2=2&input3=3 ครับ
$.ajax({
type: 'get',
url: 'test.php',
data: $('#formid').serialize()
});
$('#formid').serialize() ก็คือข้อมูล input1=1&input2=2&input3=3 ครับ
ใน key data ของคำสั่งยังสามารถส่งค่าเป็น object ได้ด้วยครับ
$.ajax({
type: 'get',
url: 'test.php',
data: {input1:1,input2:2,input3:3}
});
data: ซึ่งตรงนี้ {input1:1,input2:2,input3:3} ซึ่งเป็นผลลัพธ์ก็จะถูกแปลงเป็น input1=1&input2=2&input3=3 อีกทีหนึ่งครับ
สำหรับ jquery จะมีคำสั่งในการแปลง json string จาก server มาเป็นโครงสร้างข้อมูล javascript ครับ (รู้สึกจะใช้ eval)
$.ajax({
url:'test.php',
type:'GET',
dataType:'json',
success:function(e){
alert(e[0]+','+e[1]); //ข้อมูล json ถูกแปลงเป็นโครงสร้างข้อมูลใน javascript แล้ว
}
});
แต่ในการแปลงข้อมูลให้อยู่ในรูปแบบ json เพื่อส่งไปยัง server ตรง jquery ยังไม่มีคำสั่งนี้ครับ
แต่ก็มีคนสร้าง jquery plugin สำหรับแปลงโครงสร้างข้อมูล javascript ไปเป็น json string ไว้แล้วครับ
ดูตัวอย่างได้ที่
http://cakephp.jitwitya.com/post/asynchronous-ajax-call-php-function
================================
ฟังค์ชั่นใช้แปลงข้อมูล tis620 เป็น utf8
function utf8_all(&$item){
if (is_string($item)){
$item = iconv('TIS-620','UTF-8',$item);
} elseif (is_array($item)){
array_walk($item,'utf8_all');
} elseif (is_object($item)){
$vals = get_object_vars($item);
foreach($vals as $name=>$val){
utf8_all($item->$name);
}
}
}
|
 |
 |
 |
 |
Date :
2009-10-07 12:55:52 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มัวแต่พิมพ์ พี่หนุ่มอธิบายซะละเอียดเลย
อธิบายแบบบ้านๆ ในรูปแบบของผมที่ไม่ใช้ศัพท์มากมาย
json เป็น string format ที่สามารถแปลงเป็น object ได้ ทั้งฝั่ง client และ server ใช้ javascript รับส่ง request
สำหรับ php 5 ขึ้นไปใช้
json_encode แปลง array to json
json_decode แปลง json to array
แต่ php ต่ำกว่าต้องอาศัย library ของชาวบ้านช่วย เช่น zend เป็นต้น
ฝั่ง client ใช้ jquery ช่วยหน่อย นี่ผมใช้ jquery.json-1.3.js
$(document).ready(function(){
var data = new Object();
data.act = "major";
data.id = 1;
var JsonData = $.toJSON(data); //อันนี้แปลง object เป็น json format
$.post('json.php', {data: JsonData}, function(ret){
var obj = $.evalJSON(ret); // อันนี้แปลง string ที่ได้เป็น object
alert(obj.toSource());
});
});
เวลารับก็ใช้ POST ธรรมดานี่แหละ แล้วใช้ json_decode แปลงไปเป็น array
นี่เป็นความเข้าใจของผม ผมเข้าใจอะไรผิดเปล่าหว่า พี่หนุ่มชี้แนะน้องด้วยนะ
จริงๆ ถ้าเข้าใจ format ของ json ไม่ต้องใช้ encode ก็ได้ echo ออกมาให้ถูก format ก็ใช้ได้เหมือนกัน
|
 |
 |
 |
 |
Date :
2009-10-07 12:59:59 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
555 อั๋นนั่นแหละช่วยน้องๆ พี่แรงหมด แปวว
|
 |
 |
 |
 |
Date :
2009-10-07 13:11:39 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณเซียนพี่ทั้งสองและเจ้าของกระทู้ ที่ชี้แนะแนวทาง ครั๊ปปป
|
 |
 |
 |
 |
Date :
2009-10-07 13:22:52 |
By :
panyapol |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กระทู้นี้ร่วมเซียนหรือเปล่าครับ ได้ความรู้จากเพ่อั๋น,เพ่เอี่ยว,เพ่หนุ่ม อีกแล้ว
|
 |
 |
 |
 |
Date :
2009-10-07 14:35:33 |
By :
somparn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ซุโก่ย จริงๆๆ อ่ะ 
|
 |
 |
 |
 |
Date :
2009-10-07 14:47:42 |
By :
pokultra |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สาระเต็มๆ ชอบจังกระทู้นี้ ทั้งคนตอบและคนถาม
(ปล. add to favorite   )
|
 |
 |
 |
 |
Date :
2009-10-07 14:56:06 |
By :
peterxp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ว่าไปสลับกันดีก่านะ เปลี่ยนเปง พี่อั๋น น้อง หนุ่ม 555+
จริงป่าวลุงอั๋น
|
 |
 |
 |
 |
Date :
2009-10-07 14:59:41 |
By :
gummezaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มานมะเกี่ยวกับตาหรอกนะเค้าว่า ... อย่าบิดเบือนคดี 5555+
|
 |
 |
 |
 |
Date :
2009-10-07 16:59:41 |
By :
gummezaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอดีกลางวันรีบไปอ่านผ่านๆ และรีบถามไป ตอนนี้กลับมาอ่านดูใหม่ ก็ยังเหมือนไม่เก๊ตเท่าไหร่
คือความรู้รายละเอียดฟีเจอร์ของ javascript ผมยังไม่รู้ละเอียดมากนัก แต่ก็เอามาใช้หลายๆอย่าง
คือผมข้อนข้างสับสนเกี่ยวกับเรื่อง object ของ javascript อ่ะครับ มันไม่เหมือนเปิ้น(คนอื่น)จริงๆ
ผมลองดูรูปแบบของ json string แล้วครับ มันทำให้ผมอาจไม่ค่อยเข้าใจซักเท่าไหร่
คือผมยังไม่ได้ลองใดๆเลย เกี่ยวกับเรื่องนี้ แต่อยากจะถามอีกหน่อย(อยากลองเองแต่รู้สึกยังไงไม่รู้แฮะ)
1. ถ้าเมมเบอร์ของ {} ที่จะถูก serialize มันเป็น function() เช่น
var obj = {a: '1', b: function(){}}
แบบนี้ข้อมูลที่ถูก serialize ออกมามันจะมี b ไหม และในรูปแบบใด
ส่วนบรรทัดนี้ ของพี่ PlaKrim ผมเพิ่งเคยเห็น
var JsonData = $.toJSON(data);
ไม่ทราบว่า $.toJSON(); //อยู่ในส่วนของ utility ป่าวคับ ผมหาดูในเอกสาร chm ไม่เห็นเจอ หรือว่าจะเป็น extionsion
ในเรื่อง serialization มีเยอะคับ โดยเฉพาะภาษา oop เช่น Java(Sun) มันมีประโยชน์มากครับในภาษา oop เพราะว่า ใน class นั้นมีแอททริบิวต์เมมเบอร์ที่เป็น private, protected ซึ่งมันเป็นคุณสมบัติของ encapsulation มันไม่สามารถอ่านได้ แต่จะทำอย่างไรให้โปรแกรมบันทึกข้อมูลปัจจุบันที่อยู่ใน object ได้ ก็ต้องใช้ serialization ครับ(ต้อง implement interface Serialization มั้ง) แต่ว่ามันคงไม่เหมือนกับ ภาษาอื่น อย่าง javascript เพราะรูปแบบภาษามันต่างกัน โดยเฉพาะ Java จะมีข้อมูลเวอร์ชั่นของ class(เพื่อไม่ให้ใช้ข้อมูลเก่า มาใช้กับคลาสที่ได้ถูกแก้ไขได้ง่ายๆ) และอีกมากมาย
ขอนอกเรื่องอีกนิด ผมไม่เข้าใจว่าการสร้าง object ใน javascript นั้น ผมเห็นมีสองแบบ ซึ่งเพิ่งเคยเห็นรูปแบบที่สองไม่นานมานี้
แบบที่ 1
var obj = {a: 1, b: 2}
แบบที่ 2 (ก้อบมาจากกระทู้ OOP)
function Shape( ) {
this.getArea = function( ) {
};
}
ผมไม่รู้ความแตกต่างระหว่างแบบที่ 1 กับแบบที่ 2 แต่เคยอ่านผ่านๆ(จำไม่ได้) ว่ามันต่างกันโดยถ้าใช้ prototypeต้องใช้แบบที่ 2
ไม่ทราบว่าถ้าทำ object ตามแบบที่ 2 จะใช้งานกับ jquery หรือ serialize หรือ json ได้มั้ยครับ
ปล. นอกเรื่องไปบ้างเพราะความรู้น้อยนิด ต้องขออภัยด้วยครับ
|
 |
 |
 |
 |
Date :
2009-10-07 17:24:45 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า json_decode() ใน php จะข้ามการแปลง function b ใน json ไปเลยครับ จะเหลือแต่ข้อมูลธรรมดาๆ
ส่วน object ทั้งสองแบบน่าจะใช้ได้เหมือนกันครับ แต่คิดว่าจะไม่รวมเอา function ไป encode ครับ
แต่กลับกันถ้า php ส่งมา json มาให้ javascript ซึ่ง decode โดยใช้ eval()
ใน object ของ javascript สามารถจะประกาศ function และ run คำสั่ง javascript ได้ด้วยครับ
ซึ่งต้องระวังการนำ json มาจาก server ที่ไม่ใช่ของเรามาใช้ครับ
เพราะถ้าเค้าเขียนคำสั่ง {a:'test',b:alert('test')} ก็จะ run คำสั่ง javascript ในเครื่อง client ได้ครับ
|
 |
 |
 |
 |
Date :
2009-10-07 17:57:04 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าอย่างนั้น การส่ง json ไปให้ javascript ก็อาจจะไม่ใช่ object ของ json จริงๆก็ได้สิครับ รวมถึงข้อมูลที่อาจ error เช่น
ส่งสตริงนี้ไป
alert('ok');
document.write(...);
คือเป็น code ไม่ใช่ข้อมูล ถ้าอย่างนั้น $.getJSON() จะทำงานถูกต้องมั้ยคับ
|
 |
 |
 |
 |
Date :
2009-10-07 19:05:18 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เข้าไปดูใน jquery.js getJSON จะนำข้อมูลที่ได้รับจาก server มา eval โดยตรงครับ
if ( type == "json" )
data = window["eval"]("(" + data + ")");
สรุปมันก็คือ javascript ดีๆ นี่เองครับ รับอะไรมาก็ run หมดเลยอะ รวมทั้งข้อมูลที่ผิดพลาดด้วยครับ
|
 |
 |
 |
 |
Date :
2009-10-07 19:52:07 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาอีกๆ เก็บเข้าหมอง 
|
 |
 |
 |
 |
Date :
2009-10-07 20:36:59 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
data = window["eval"]("(" + data + ")"); // ดูมันทำ เพิ่งเคยเห็น 
ใช้ วงเล็บคร่อมทำไม มันได้อะไรขึ้นมา 
|
 |
 |
 |
 |
Date :
2009-10-07 20:49:52 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|