永发信息网

JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告

答案:1  悬赏:70  手机版
解决时间 2021-03-16 18:20
  • 提问者网友:沉默的哀伤
  • 2021-03-16 03:12
JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告
最佳答案
  • 五星知识达人网友:西风乍起
  • 2019-08-23 08:30
以下例子都会出现乱码, 虽然都可以有变通的方案, 但如果不乱码就太好了! 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-?} 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.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯