StringToJSONString

StringToJSONString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function StringToJSONString(const S: string; strict: Boolean = False): string;
var
  I, J, L: Integer;
  P: PChar;
  C: Char;
begin
  I := 1;
  J := 1;
  Result := '';
  L := Length(S);
  P := PChar(S);
  while I <= L do
  begin
    C := Char(P^);
    Result := Result + Copy(S, J, I - J);
    if (C in ['a'..'z']) or (C in ['A'..'Z']) or (C in ['0'..'9']) or (C = ':')
      or (c='{') or (c='}') or (c='"') or (c='[') or (c=']') or (c=','
      ) then
      Result := Result + C
    else
    case C of
      '\':
        Result := Result + '\\';
      '/':
        if strict then
          Result := Result + '\/'
        else
          Result := Result + '/';
      #8:  //退格
        Result := Result + '\b';
      #9:  //水平定位符号
        Result := Result + '\t';
      #10: //换行键
        Result := Result + '\n';
      #12: //换页键
        Result := Result + '\f';
      #13: //回车
        Result := Result + '\r';
    else  //汉字
      Result := Result + '\u' + IntToHex(Ord(C), 4);
    end;
    J := I + 1;
    Inc(I);
    Inc(P);
  end;
  Result := Result + Copy(S, J, I - 1);
end;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function JSONStringToString(const S: string): string;
var
  I, J, L: Integer;
  P: PChar;
  w: string;
begin
  I := 1;
  J := 1;
  L := Length(S);
  Result := '';
  P := PChar(S);
  while (I <= L) do
  begin
    if (P^ = '\') then
    begin
      Result := Result + Copy(S, J, I - J);
      Inc(P);
      if (P^ <> #0) then
      begin
        Inc(I);
        case Char(P^) of
          '\', '"', '/':
            Result := Result + P^;
          'b':
            Result := Result + #8;
          't':
            Result := Result + #9;
          'n':
            Result := Result + #10;
          'f':
            Result := Result + #12;
          'r':
            Result := Result + #13;
          'u':
            begin
              w := Copy(S, I + 1, 4);
              Inc(I, 4);
              Inc(P, 4);
              Result := Result + Char(StrToInt('$' + w));
            end;
        end;
      end;
      J := I + 1;
    end;
    Inc(I);
    Inc(P);
  end;
  Result := Result + Copy(S, J, I - J + 1);
end;

  

 

1
2
3
4
5
6
7
8
procedure TForm1.Button1Click(Sender: TObject);
begin
  var s: string;
  s := StringToJSONString('{"Key1":"张三"}');
  memo2.Lines.Add(s);  //{"Key1":"\u5F20\u4E09"}
  s := JSONStringToString(s);  //{"Key1":"张三"}
  memo2.Lines.Add(s);
end;

  

  

posted @   delphi中间件  阅读(540)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2016-12-01 离别 李叔同
2014-12-01 咏南中间件支持DELPHI低版本开发的两层程序平稳升级到三层
2013-12-01 代理服务器
点击右上角即可分享
微信分享提示