superobject组装json的一个demo:
var JSON, item, ArrayObj: ISuperObject; begin JSON := SO('{}'); item := SO('{}'); item.S['role'] := 'user'; item.S['content'] := '这是添加的内容'; ArrayObj := SA([]); JSON.O['messages'] := ArrayObj; JSON.A['messages'].Add(item); szJsonData:= JSON.AsString;
下面是A2W和W2A的代码。
function AnsiToUtf8(const AnsiStr: string): string; var WideStr: WideString; WideLen, Utf8Len: Integer; begin // 将 ANSI 字符串转换为宽字符字符串 WideLen := MultiByteToWideChar(CP_ACP, 0, PAnsiChar(AnsiStr), Length(AnsiStr), nil, 0); SetLength(WideStr, WideLen); MultiByteToWideChar(CP_ACP, 0, PAnsiChar(AnsiStr), Length(AnsiStr), PWideChar(WideStr), WideLen); // 将宽字符字符串转换为 UTF-8 字符串 Utf8Len := WideCharToMultiByte(CP_UTF8, 0, PWideChar(WideStr), WideLen, nil, 0, nil, nil); SetLength(Result, Utf8Len); WideCharToMultiByte(CP_UTF8, 0, PWideChar(WideStr), WideLen, PChar(Result), Length(Result), nil, nil); end; function UTF8ToANSI(const UTF8Str: string): string; var WideStr: WideString; begin //将UTF8转宽字符 SetLength(WideStr, MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(UTF8Str), -1, nil, 0)); MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(UTF8Str), -1, PWideChar(WideStr), Length(WideStr)); //将宽字符转Ansi SetLength(Result, WideCharToMultiByte(CP_ACP, 0, PWideChar(WideStr), -1, nil, 0, nil, nil)); WideCharToMultiByte(CP_ACP, 0, PWideChar(WideStr), -1, PAnsiChar(Result), Length(Result), nil, nil); end;
34 评论