線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1477
推到 Plurk!
推到 Facebook!

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

缺席
mitchellhu
一般會員


發表:23
回覆:53
積分:15
註冊:2007-06-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-12-11 22:14:29 IP:123.204.xxx.xxx 訂閱
 依據 捷庫出版李維所著Delph XE 程式設計手冊一書,寫了一支 RestDataSnapServer 測試。
結果正常,但JSON string 內由資料庫取的中文資料出現亂碼:
Executed:
TServerMethods1.GetexamlistJson
;

在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
一般會員


發表:23
回覆:53
積分:15
註冊: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中文了。很抱歉,打擾大家了。
系統時間:2024-03-29 15:44:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!