全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1054
推到 Plurk!
推到 Facebook!

RestDataSnapSever 連結mysql db, JSON result 中文亂碼問題

缺席
mitchellhu
一般會員


發表:22
回覆:51
積分:14
註冊:2007-06-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-12-11 22:14:29 IP:123.204.xxx.xxx 訂閱
 依據 捷庫出版李維所著Delph XE 程式設計手冊一書,寫了一支 RestDataSnapServer 測試。
結果正常,但JSON string 內由資料庫取的中文資料出現亂碼:
Executed:TServerMethods1.GetexamlistJson
Result:{"result":[{"單號":"101-04-011","檢查類別":"??","識別編號":"1","序號":"1","品名":"??????(?1?/?)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"10","序號":"10","品名":"???????(ATEGO)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"11","序號":"11","品名":"???????(ATEGO/ACTROS) ","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"12","序號":"12","品名":"??????(ATEGO) ","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"13","序號":"13","品名":"??????????(?1??/?)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"14","序號":"14","品名":"???/????? (?1?0.7L/?)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"15","序號":"15","品名":"??????(?1?0.5L/?)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"16","序號":"16","品名":"?????","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"17","序號":"17","品名":"??????? (?1?1.5L/?)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"18","序號":"18","品名":"?????????????(?1?/?)","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類別":"??","識別編號":"19","序號":"19","品名":"??????? ","檢驗說明":"??????????????","檢驗方式":"????","檢驗結果":"","檢驗單位":"??/??"},{"單號":"101-04-011","檢查類

Mysql table 的設定 是UTF8
CREATE TABLE `examelist` (
`docno` varchar(15) NOT NULL COMMENT '申請單號',
`itemtype` varchar(14) NOT NULL,
`id` varchar(10) DEFAULT NULL,
`itemno` int(4) NOT NULL,
`item` varchar(255) NOT NULL,
`examscript` varchar(255) NOT NULL,
`examtype` varchar(4) NOT NULL,
`exameresult` varchar(3) DEFAULT '合格',
`signunit` varchar(10) NOT NULL COMMENT '適用裝備型式',
`pic1` longblob COMMENT '維修前圖片',
`pic2` longblob COMMENT '維修中圖片',
`pic3` longblob COMMENT '維修後圖片'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在servermethodUnit中Function code 如下:

function TServerMethods1.GetexamlistJson: TJSONArray;
var
Ja : TJSONArray;
Jo : TJSONObject;
Jp : TJSONPair;
aaaa : String;
begin
examelist.Active := False;
examelist.CommandText := 'select * from examelist where docno = "101-04-011" ';
try
examelist.Active := True;
examelist.First;
Ja := TJSONArray.Create;
while not examelist.Eof do
begin
Jo := TJSONObject.Create;
Jo.AddPair(TJSONString.Create('單號'),TJSONString.Create(examelist.FieldByName('docno').AsString));
Jo.AddPair(TJSONString.Create('檢查類別'),TJSONString.Create(examelist.FieldByName('itemtype').AsString));
Jo.AddPair(TJSONString.Create('識別編號'),TJSONString.Create(examelist.FieldByName('id').AsString));
Jo.AddPair(TJSONString.Create('序號'),TJSONString.Create(examelist.FieldByName('itemno').AsString));
Jo.AddPair(TJSONString.Create('品名'),TJSONString.Create(examelist.FieldByName('item').AsString));
Jo.AddPair(TJSONString.Create('檢驗說明'),TJSONString.Create(examelist.FieldByName('examscript').AsString));
Jo.AddPair(TJSONString.Create('檢驗方式'),TJSONString.Create(examelist.FieldByName('examtype').AsString));
Jo.AddPair(TJSONString.Create('檢驗結果'),TJSONString.Create(examelist.FieldByName('exameresult').AsString));
Jo.AddPair(TJSONString.Create('檢驗單位'),TJSONString.Create(examelist.FieldByName('signunit').AsString));
Ja.AddElement(Jo);
examelist.Next;
end;
finally
result := Ja;
end;
end;

連結資料庫的部份是使用 dbexpress 元件
mysqlconn: TSQLConnection;
examelist: TSQLDataSet;
我另外有用 dbgrid 連接測試,結果中文正常。

請問各位先進,可否指導何處設定有誤導致中文顯示正常。
謝謝!




編輯記錄
mitchellhu 重新編輯於 2012-12-11 07:16:21, 註解 無‧
mitchellhu
一般會員


發表:22
回覆:51
積分:14
註冊:2007-06-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2012-12-12 00:38:38 IP:123.204.xxx.xxx 訂閱
 sorry,剛才想到,用dbexpress 連接 dbgrid可正常,會不會是因為 restdatasnap這邊的dbconnetion 設定有所不同。
後來檢查,果真我用到xe2 內附的mysql.dll ,這支dll從來都沒正確過。我改用自購的msql .dll後就可正常顯示正確的
unicode中文了。很抱歉,打擾大家了。
系統時間:2017-10-24 19:32:40
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!