以下例子都会出现乱码, 虽然都可以有变通的方案, 但如果不乱码就太好了!
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses SuperObject; procedure TForm1.Button1Click(Sender: TObject); var jo: ISuperObject; stream: TStream; begin stream := TMemoryStream.Create; jo := SO(‘{"Language" : "简体中文"}‘); jo.SaveTo(stream, False, False); stream.Position := 0; Memo1.Lines.LoadFromStream(stream); // {"Language":"€S-?} stream.Free; end; procedure TForm1.Button2Click(Sender: TObject); const FilePath = ‘C:\Temp\Test.txt‘; var jo: ISuperObject; begin jo := SO(‘{"Language" : "简体中文"}‘); jo.SaveTo(FilePath, False, False); with TStringList.Create do begin LoadFromFile(FilePath); ShowMessage(Text); // {"Language":"€S-?} Free; end; end; procedure TForm1.Button3Click(Sender: TObject); var jo: ISuperObject; stream: TStream; begin stream := TStringStream.Create(‘{"Language" : "简体中文"}‘); jo := TSuperObject.ParseStream(stream); ShowMessage(jo.AsJSon(False, False)); // {"Language":"¼òÌåÖÐÎÄ"} stream.Free; end; procedure TForm1.Button4Click(Sender: TObject); const FilePath = ‘C:\Temp\Test.txt‘; var jo: ISuperObject; begin with TStringList.Create do begin Add(‘{"Language" : "简体中文"}‘); SaveToFile(FilePath); Free; end; jo := TSuperObject.ParseFile(FilePath); ShowMessage(jo.AsJSon(False, False)); // {"Language":"¼òÌåÖÐÎÄ"} end; end.
下面是以上问题的暂时的变通方案:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses SuperObject; procedure TForm1.Button1Click(Sender: TObject); var jo: ISuperObject; stream: TStream; begin stream := TMemoryStream.Create; jo := SO(‘{"Language" : "简体中文"}‘); jo.SaveTo(stream, False, True); stream.Position := 0; Memo1.Lines.LoadFromStream(stream); // {"Language":"\u7b80\u4f53\u4e2d\u6587"} stream.Position := 0; jo := TSuperObject.ParseStream(stream); ShowMessage(jo.AsString); //{"Language" : "简体中文"} stream.Free; end; procedure TForm1.Button2Click(Sender: TObject); const FilePath = ‘C:\Temp\Test.txt‘; var jo: ISuperObject; begin jo := SO(‘{"Language" : "简体中文"}‘); jo.SaveTo(FilePath, False, True); with TStringList.Create do begin LoadFromFile(FilePath); ShowMessage(Text); // {"Language":"\u7b80\u4f53\u4e2d\u6587"} Free; end; end; procedure TForm1.Button3Click(Sender: TObject); var jo: ISuperObject; stream: TStream; begin stream := TStringStream.Create(‘{"Language":"\u7b80\u4f53\u4e2d\u6587"}‘); jo := TSuperObject.ParseStream(stream); ShowMessage(jo.AsJSon(False, False)); // {"Language" : "简体中文"} stream.Free; end; procedure TForm1.Button4Click(Sender: TObject); const FilePath = ‘C:\Temp\Test.txt‘; var jo: ISuperObject; begin with TStringList.Create do begin Add(‘{"Language" : "简体中文"}‘); SaveToFile(FilePath); Free; end; Memo1.Lines.LoadFromFile(FilePath); jo := SO(Memo1.Text); // jo := TSuperObject.ParseFile(FilePath); ShowMessage(jo.AsJSon(False, False)); // {"Language" : "简体中文"} end; end.
时间: 2024-10-11 09:00:12