Delphi 的Json解析组件 SuperObject

SuperObject 构建一个 JSON 的常用方法: 从字符串、从文件、从流.

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;
    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;

const JsonStr = '{"No1":"张三", "No2":"李四"}';

//从字符串构建
procedure TForm1.Button1Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := SO(JsonStr);
  {或者用使用下面语句, SO 函数就是调用了 TSuperObject.ParseString}
  //jo := TSuperObject.ParseString(JsonStr);
  ShowMessage(jo.AsJSon(True, False));
end;

//从文件构建
procedure TForm1.Button2Click(Sender: TObject);
const
  path = 'c:\temp\json.txt';
var
  jo: ISuperObject;
begin
  {产生个测试文件; SuperObject 对中文支持也不太好, 读取它自己保存的文件吧}
  SO(JsonStr).SaveTo(path); {这就产生并保存了 json 文件}

  jo := TSuperObject.ParseFile(path);
  ShowMessage(jo.AsJSon(True, False));
end;

//从流构建
procedure TForm1.Button3Click(Sender: TObject);
var
  jo: ISuperObject;
  stm: TStream;
  b: Byte;
begin
  {模拟个测试流; 看看它能接受的编码够原始的, 它存取文件也是如此}
  stm := TStringStream.Create('{"No2":"\u674e\u56db","No1":"\u5f20\u4e09"}');

  jo := TSuperObject.ParseStream(stm);
  ShowMessage(jo.AsJSon(True, False));

  stm.Free;
end;

//AsJSon 的参数
procedure TForm1.Button4Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := SO(JsonStr);

  ShowMessage(jo.AsJSon);
  ShowMessage(jo.AsJSon(True));
  ShowMessage(jo.AsJSon(True, False));
  ShowMessage(jo.AsJSon(False, False));
end;

end.

万一
关注 - 33
粉丝 - 126
关注博主
0
0
0
(请您对文章做出评价)
« 上一篇:JSON 之 SuperObject(1)
» 下一篇:JSON 之 SuperObject(3): 访问
posted on 2009-10-23 13:12 万一 阅读(1216) 评论(3) 编辑 收藏 所属分类: 47.Delphi 与 JOSN

FeedBack:
1703609
#1楼 2009-11-20 23:49 joy.zhao[未注册用户]
修改superobject 单元
TSuperWriterStream.Append 函数为这样
调用
SO(JsonStr).SaveTo(path,true,False); {这就产生并保存了 json 文件}
存储文件,就可以显示正常的中文了
我在delphi7下测试ok

function TSuperWriterStream.Append(buf: PSOChar; Size: Integer): Integer;
{$IFDEF UNICODE}
var
// Buffer: array[0..1023] of AnsiChar;
// pBuffer: PAnsiChar;
// i: Integer;
sAnsiString:AnsiString;
{$ENDIF}
begin
{$IFDEF UNICODE}
sAnsiString:=WideCharLenToString(buf,Size);
Result := FStream.Write(sAnsiString[1], Length(sAnsiString));

// if Size = 1 then
// Result := FStream.Write(buf^, Size) else
// begin
// if Size > SizeOf(Buffer) then
// GetMem(pBuffer, Size) else
// pBuffer := @Buffer;
// try
// for i := 0 to Size - 1 do
// pBuffer[i] := AnsiChar(buf[i]);
// Result := FStream.Write(pBuffer^, Size);
// finally
// if pBuffer <> @Buffer then
// FreeMem(pBuffer);
// end;
// end;
{$ELSE}
Result := FStream.Write(buf^, Size * SizeOf(SOChar));
{$ENDIF}
end;

修改superobject 单元
TSuperWriterStream.ParseStream函数为这样
可以读取中文文件

修改superobject 单元
TSuperWriterStream.Append 函数为这样
调用
SO(JsonStr).SaveTo(path,true,False); {这就产生并保存了 json 文件}
存储文件,就可以显示正常的中文了
我在delphi7下测试ok

function TSuperWriterStream.Append(buf: PSOChar; Size: Integer): Integer;
{$IFDEF UNICODE}
var
// Buffer: array[0..1023] of AnsiChar;
// pBuffer: PAnsiChar;
// i: Integer;
sAnsiString:AnsiString;
{$ENDIF}
begin
{$IFDEF UNICODE}
sAnsiString:=WideCharLenToString(buf,Size);
Result := FStream.Write(sAnsiString[1], Length(sAnsiString));

// if Size = 1 then
// Result := FStream.Write(buf^, Size) else
// begin
// if Size > SizeOf(Buffer) then
// GetMem(pBuffer, Size) else
// pBuffer := @Buffer;
// try
// for i := 0 to Size - 1 do
// pBuffer[i] := AnsiChar(buf[i]);
// Result := FStream.Write(pBuffer^, Size);
// finally
// if pBuffer <> @Buffer then
// FreeMem(pBuffer);
// end;
// end;
{$ELSE}
Result := FStream.Write(buf^, Size * SizeOf(SOChar));
{$ENDIF}
end;

修改superobject 单元
TSuperWriterStream.ParseStream函数为这样
可以读取中文文件

class function TSuperObject.ParseStream(stream: TStream; partial: boolean; const this: ISuperObject; options: TSuperFindOptions;
const put: ISuperObject; dt: TSuperType): ISuperObject;
const
BUFFER_SIZE = 1024;
var
tok: TSuperTokenizer;
buffera: array[0..BUFFER_SIZE-1] of AnsiChar;
{$IFDEF UNICODE}
// bufferw: array[0..BUFFER_SIZE-1] of SOChar;
ws:WideString;
{$ENDIF}
j, size: Integer;
st: string;
begin
st := '';
tok := TSuperTokenizer.Create;
size := stream.Read(buffera, BUFFER_SIZE);
while size > 0 do
begin
{$IFDEF UNICODE}
ws:= buffera;
ParseEx(tok, PWideChar(ws), StrLen(PWideChar(ws)), this, options, put, dt);

// for j := 0 to size - 1 do
// bufferw[j] := SOChar(buffera[j]);
// ParseEx(tok, bufferw, size, this, options, put, dt);
{$ELSE}
ParseEx(tok, buffera, size, this, options, put, dt);
{$ENDIF}
if tok.err = teContinue then
size := stream.Read(buffera, BUFFER_SIZE) else
Break;
end;
if(tok.err <> teSuccess) or (not partial and (st[tok.char_offset] <> #0)) then
Result := nil else
Result := tok.stack[tok.depth].current;
tok.Free;
end;


发表评论

电子邮件地址不会被公开。 必填项已用*标注

55 − = 46