【问题标题】:Edit JSON array with Inno Setup使用 Inno Setup 编辑 JSON 数组
【发布时间】:2021-07-12 13:45:34
【问题描述】:

我想用 Inno Setup 编辑一个 JSON 文件来输入我的程序的安装路径。

我也找到了这个帖子:How to parse a JSON string in Inno Setup? 但是,我无法应付它,因为它谈到了InfoUserString,但我只有InfoUser

这是需要编辑的条目的样子:

  "game_dirs": [
    "Installation path"
  ]

这是我的代码:

[Code]
function JSONQueryString(FileName, Section, Key, Default: WideString;
  var Value: WideString; var ValueLength: Integer): Boolean;
  external 'JSONQueryString@files:jsonconfig.dll stdcall';
function JSONQueryBoolean(FileName, Section, Key: WideString; 
  Default: Boolean; var Value: Boolean): Boolean;
  external 'JSONQueryBoolean@files:jsonconfig.dll stdcall';
function JSONQueryInteger(FileName, Section, Key: WideString; 
  Default: Int64; var Value: Int64): Boolean;
  external 'JSONQueryInteger@files:jsonconfig.dll stdcall';
function JSONWriteString(FileName, Section, Key, 
  Value: WideString): Boolean;
  external 'JSONWriteString@files:jsonconfig.dll stdcall';
function JSONWriteBoolean(FileName, Section, Key: WideString;
  Value: Boolean): Boolean;
  external 'JSONWriteBoolean@files:jsonconfig.dll stdcall';
function JSONWriteInteger(FileName, Section, Key: WideString;
  Value: Int64): Boolean;
  external 'JSONWriteInteger@files:jsonconfig.dll stdcall';

function BoolToStr(Value: Boolean): string;
begin
  Result := 'True';
  if not Value then
    Result := 'False';
end;

procedure InitializeWizard;
var
  FileName: WideString;
  IntValue: Int64;
  StrValue: WideString;
  StrLength: Integer;
  BoolValue: Boolean;
begin
  { set the source JSON config file path }
  FileName := '{app}\\PATH\\Config.json';
  { allocate string buffer to enough length }
  SetLength(StrValue, 16);
  { set the buffer length value }
  StrLength := Length(StrValue);
  { query string value }
  if JSONQueryString(FileName, 'game_dirs', '{app}\TEST', 'Default', StrValue, 
    StrLength)
  then
    MsgBox('Section_1:Key_1=' + StrValue, mbInformation, MB_OK);
  { query integer value }
  if JSONQueryInteger(FileName, 'Section_1', 'Key_2', 0, IntValue) then
    MsgBox('Section_1:Key_2=' + IntToStr(IntValue), mbInformation, MB_OK);
  { query boolean value }
end;

安装过程中出现以下错误信息:

运行时错误(在 46:224):无法调用 proc。

【问题讨论】:

    标签: json inno-setup pascalscript


    【解决方案1】:

    您的 game_dirs 元素是一个 JSON 数组。我不认为JSONConfig.dll 支持数组。

    您可以改用JsonParser library。使用我对How to parse a JSON string in Inno Setup? 的回答中的附加功能,代码可以是:

    function FindJsonArray(
      Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
      var Arr: TJsonArray): Boolean;
    var
      JsonValue: TJsonValue;
    begin
      Result :=
        FindJsonValue(Output, Parent, Key, JsonValue) and
        (JsonValue.Kind = JVKArray);
      if Result then
      begin
        Arr := Output.Arrays[JsonValue.Index];
      end;
    end;
    
    procedure EditGameDirs(FileName: string; GameDir: string);
    var
      JsonLines: TStringList;
      JsonParser: TJsonParser;
      JsonRoot: TJsonObject;
      GameDirs: TJsonArray;
    begin
      JsonLines := TStringList.Create;
      JsonLines.LoadFromFile(FileName);
    
      if ParseJsonAndLogErrors(JsonParser, JsonLines.Text) then
      begin
        JsonRoot := GetJsonRoot(JsonParser.Output);
        if not FindJsonArray(JsonParser.Output, JsonRoot, 'game_dirs', GameDirs) then
        begin
          Log('Cannot find game_dirs');
        end
          else
        if (GetArrayLength(GameDirs) <> 1) or
           (GameDirs[0].Kind <> JVKString) then
        begin
          Log('game_dirs does not have one string element');
        end
          else
        begin
          Log(Format('Previous game_dirs was [%s]', [
            JsonParser.Output.Strings[GameDirs[0].Index]]));
          Log(Format('New game_dirs is [%s]', [GameDir]));
          JsonParser.Output.Strings[GameDirs[0].Index] := GameDir;
          JsonLines.Clear;
          PrintJsonParserOutput(JsonParser.Output, JsonLines);
          JsonLines.SaveToFile(FileName + '2');
        end;
      end;
    end;  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      相关资源
      最近更新 更多