XE3随笔8:关于乱码

以下例子都会出现乱码, 虽然都可以有变通的方案, 但如果不乱码就太好了!


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

XE3随笔8:关于乱码的相关文章

XE3随笔20:几个和当前路径相关的新函数

偶然从 SysUtils 里发现了几个路径相关的函数, 以前没见过, 可能是 Delphi XE3 新增的: GetLocaleDirectory(); GetLocaleFile(); LocaleDirectoryExists(); LocaleFileExists(); 应用测试: uses IOUtils; procedure TForm1.FormCreate(Sender: TObject); var   p,f: string;   b: Boolean; begin   {设置与

XE3随笔2:SuperObject构建JSON

SuperObject 构建一个 JSON 的常用方法: 从字符串.从文件.从流. unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Button1: TButton;     Button2: TButton;     Button

XE3随笔15:使用 IXMLHTTPRequest 简单获取网页源代码

unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Memo1: TMemo;     Button1: TButton;     procedure Button1Click(Sender: TObject);   end; var

XE3随笔3:访问

测试数据提前加入 Memo1 中: { "name": "张三", /* 注释 */ "age": 33, "sex": true, "weight": 123.456, "tel": ["86-1111111", "86-2222222"], "addresses":{"address":"A省B

XE3随笔17:实例 - 模拟 Google 搜索

本例测试效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Memo1: TMemo;     Edit1: TEdit;     Button1: TButton;     procedure Button1Cl

XE3随笔19:实例 - 借用 Google 实现全文翻译

调用 Google 翻译的地址格式: http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + 原始文本 + "&langpair=" + 原语言 + "%7C" + 目标语言 返回的数据格式如下, 可以用 responseData.translatedText 简单获取: { "responseData" : {"trans

XE3随笔5:Format与转义字符

unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Button1: TButton;     Button2: TButton;     procedure Button1Click(Sender: TObject);     pro

XE3随笔9:使用不同的数据类型标记数组

unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Button1: TButton;     Button2: TButton;     Button3: TButton;     Button4: TButton;     proc

XE3随笔21:系统默认语言与系统支持的语言列表

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: