delphi JSON在带有转义/特殊字符的JSON之间来回转换字符串  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi JSON在带有转义/特殊字符的JSON之间来回转换字符串


uses system.JSON;


function JsonEncodeString(const AStr: string): string;

var

  LStr: TJSONString;

begin

  LStr := TJSONString.Create(AStr);

  try

    Result := LStr.ToJSON;

    Result := Result.Substring(1, Result.Length - 2);

  finally

    LStr.Free;

  end;

end;


procedure TForm1.FormCreate(Sender: TObject);

begin

text:=JsonEncodeString('"abc"');

end;



代码2:

uses

  DBXJSON;


type

  TSvJsonString = class(TJSONString)

  private

    function EscapeValue(const AValue: string): string;

  public

    constructor Create(const AValue: string); overload;

  end;


{ TSvJsonString }


constructor TSvJsonString.Create(const AValue: string);

begin

  inherited Create(EscapeValue(AValue));

end;


function TSvJsonString.EscapeValue(const AValue: string): string;


  procedure AddChars(const AChars: string; var Dest: string; var AIndex: Integer); inline;

  begin

    System.Insert(AChars, Dest, AIndex);

    System.Delete(Dest, AIndex + 2, 1);

    Inc(AIndex, 2);

  end;


  procedure AddUnicodeChars(const AChars: string; var Dest: string; var AIndex: Integer); inline;

  begin

    System.Insert(AChars, Dest, AIndex);

    System.Delete(Dest, AIndex + 6, 1);

    Inc(AIndex, 6);

  end;


var

  i, ix: Integer;

  AChar: Char;

begin

  Result := AValue;

  ix := 1;

  for i := 1 to System.Length(AValue) do

  begin

    AChar :=  AValue[i];

    case AChar of

      '/', '\', '"':

      begin

        System.Insert('\', Result, ix);

        Inc(ix, 2);

      end;

      #8:  //backspace \b

      begin

        AddChars('\b', Result, ix);

      end;

      #9:

      begin

        AddChars('\t', Result, ix);

      end;

      #10:

      begin

        AddChars('\n', Result, ix);

      end;

      #12:

      begin

        AddChars('\f', Result, ix);

      end;

      #13:

      begin

        AddChars('\r', Result, ix);

      end;

      #0 .. #7, #11, #14 .. #31:

      begin

        AddUnicodeChars('\u' + IntToHex(Word(AChar), 4), Result, ix);

      end

      else

      begin

        if Word(AChar) > 127 then

        begin

          AddUnicodeChars('\u' + IntToHex(Word(AChar), 4), Result, ix);

        end

        else

        begin

          Inc(ix);

        end;

      end;

    end;

  end;

end;


您可以尝试定义自己的TJSONString类型,并在其中转义json字符串。例如:


uses

  DBXJSON;


type

  TSvJsonString = class(TJSONString)

  private

    function EscapeValue(const AValue: string): string;

  public

    constructor Create(const AValue: string); overload;

  end;


{ TSvJsonString }


constructor TSvJsonString.Create(const AValue: string);

begin

  inherited Create(EscapeValue(AValue));

end;


function TSvJsonString.EscapeValue(const AValue: string): string;


  procedure AddChars(const AChars: string; var Dest: string; var AIndex: Integer); inline;

  begin

    System.Insert(AChars, Dest, AIndex);

    System.Delete(Dest, AIndex + 2, 1);

    Inc(AIndex, 2);

  end;


  procedure AddUnicodeChars(const AChars: string; var Dest: string; var AIndex: Integer); inline;

  begin

    System.Insert(AChars, Dest, AIndex);

    System.Delete(Dest, AIndex + 6, 1);

    Inc(AIndex, 6);

  end;


var

  i, ix: Integer;

  AChar: Char;

begin

  Result := AValue;

  ix := 1;

  for i := 1 to System.Length(AValue) do

  begin

    AChar :=  AValue[i];

    case AChar of

      '/', '\', '"':

      begin

        System.Insert('\', Result, ix);

        Inc(ix, 2);

      end;

      #8:  //backspace \b

      begin

        AddChars('\b', Result, ix);

      end;

      #9:

      begin

        AddChars('\t', Result, ix);

      end;

      #10:

      begin

        AddChars('\n', Result, ix);

      end;

      #12:

      begin

        AddChars('\f', Result, ix);

      end;

      #13:

      begin

        AddChars('\r', Result, ix);

      end;

      #0 .. #7, #11, #14 .. #31:

      begin

        AddUnicodeChars('\u' + IntToHex(Word(AChar), 4), Result, ix);

      end

      else

      begin

        if Word(AChar) > 127 then

        begin

          AddUnicodeChars('\u' + IntToHex(Word(AChar), 4), Result, ix);

        end

        else

        begin

          Inc(ix);

        end;

      end;

    end;

  end;

end;

用法示例:


procedure Test;

var

  LText, LEscapedText: string;

  LJsonString: TSvJsonString;

  LJsonPair: TJsonPair;

  LJsonObject: TJsonObject;

begin

  LText := 'c:\path\name' + #13 + #10 + 'Next Line';

  LJsonString := TSvJsonString.Create(LText);

  LJsonPair := TJsonPair.Create('MyString', LJsonString);

  LJsonObject := TJsonObject.Create(LJsonPair);

  try

    LEscapedText := LJsonObject.ToString;

    //LEscapedText is: c:\\path\\name\r\nNext Line

  finally

    LJsonObject.Free;

  end;

end;

这就是解析的方式:


//AText := '{"MyString":"c:\\path\\name\r\nNext Line"}';

function Parse(const AText: string): string;

var

  obj: TJSONValue;

begin

  obj := TJSONObject.ParseJSONValue(AText);

  try

    Result := obj.ToString;

    //Result := {"MyString":"c:\path\name

   //Next Line"}

  finally

    obj.Free;

  end;

end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.11634182929993 seconds