Delphi解析类似\u97e9这样的Unicode字符串

使用Delphi对Json中的Unicode字符串进行解析:
[{"name":"\u97e9\u56fd\u9996\u5c14KT\u670d\u52a1\u5668","ip":"211.115.67.40"},{"name":"\u97e9\u56fd\u5357\u90e8","ip":"211.115.67.40"}]
 
function TForm1.DecodeUnicodeString(const S: string): string;
var
  buf: array[0..4] of Char;
  i: Integer;
  Ch, PvCh: Char;
  US: string;
  Str: string;
begin
  Result := '';
  ZeroMemory(@buf[0], SizeOf(buf));
  i := 1;
  while i < Length(S) do
  begin
    Ch := S[i];
    if (Ch = 'u') and (i > 1) then
    begin
      PvCh := S[i-1];
      if PvCh = '\' then
      begin
        US := Copy(S, i+3, 2) + Copy(S, i+1, 2);
        HexToBin(PChar(US), buf, 2);
        Str := WideCharToString(@buf[0]);
        Result := Copy(Result, 1, Length(Result)-1);  // drop "\"
        Result := Result + Str;
        Inc(i, 5);
        Continue;
      end;
    end;
    Inc(i, 1);
    Result := Result + Ch;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowMessage(DecodeUnicodeString('\u97e9\u56fd\u9996\u5c14KT\u670d\u52a1\u5668'));
end;

运行结果

原文地址:https://www.cnblogs.com/ddgg/p/2952866.html