【问题标题】:Inno Setup: Change value in JSON arrayInno Setup:更改 JSON 数组中的值
【发布时间】:2017-12-14 12:31:51
【问题描述】:

我想在 Inno Setup 的帮助下创建一个安装程序。为了使程序在每台计算机上运行,​​我需要更改.json 文件中的工具目录。以下是该文件的摘录:

{
  "commandScriptLinux" : "",
  "copyToolBehavior" : "once",
  "deleteWorkingDirectoriesAfterWorkflowExecution" : true,
  "deleteWorkingDirectoriesKeepOnErrorOnce" : true,
  "deleteWorkingDirectoriesNever" : true,
  "documentationFilePath" : "",
  "enableCommandScriptWindows" : true,
  "imitationScript" : "",
  "imitationToolOutputFilename" : "",
  "launchSettings" :
  [
    {
      "limitInstallationInstancesNumber" : "1",
      "limitInstallationInstances" : "false",
      "toolDirectory" : "%Selected Setup Folder%",
      "version" : "1.0"
    }
  ],
}

我希望通过使用inno-json-config 库来解决这个问题。不幸的是,在执行代码之后,这些行被颠倒了(现在最后一行出现)并且没有进行更改。

[Setup]
AppName=Change_Config
AppVersion=1.0
DefaultDirName={userdocs}\Change_Config

[Files]
Source: "JSONConfig.dll"; Flags: dontcopy

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

procedure InitializeWizard;
var
  FileName: WideString;
  IntValue: Int64;
  StrValue: WideString;
  StrLength: Integer;
  BoolValue: Boolean;
begin
  FileName := 'c:\configuration.json';
  SetLength(StrValue, 16);
  StrLength := Length(StrValue);

  if JSONQueryString(
       FileName, 'launchSettings', 'toolDirectory', 'Default', StrValue, StrLength) then
    MsgBox('Section_1:Key_1=' + StrValue, mbInformation, MB_OK);

  if not JSONWriteString(FileName, 'launchSettings', 'toolDirectory', 'Test') then
    MsgBox('JSONWriteString Section_1:Key_1 failed!', mbError, MB_OK);
end;

非常感谢您的支持!

问候, 亚历克斯

【问题讨论】:

    标签: json inno-setup


    【解决方案1】:

    launchSettings 是一个数组。我相信inno-json-config 库不支持数组。

    您可以改用JsonParser library

    [Code]
    
    #include "JsonParser.pas"
    
    function FindJsonValue(
      Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
      var Value: TJsonValue): Boolean;
    var
      I: Integer;
    begin
      for I := 0 to Length(Parent) - 1 do
      begin
        if Parent[I].Key = Key then
        begin
          Value := Parent[I].Value;
          Result := True;
          Exit;
        end;
      end;
    
      Result := False;
    end;
    
    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;
    
    { ... }
    
    var
      JsonLines: TStringList;
      JsonParser: TJsonParser;
      LaunchSettingsArray: TJsonArray;
      ToolDirectoryValue: TJsonValue;
      I: Integer;
    begin
      { ... }
    
      JsonLines := TStringList.Create;
      JsonLines.LoadFromFile(FileName);
    
      ParseJson(JsonParser, JsonLines.Text);
    
      if Length(JsonParser.Output.Errors) > 0 then
      begin
        Log('Error parsing JSON');
        for I := 0 to Length(JsonParser.Output.Errors) - 1 do
        begin
          Log(JsonParser.Output.Errors[I]);
        end;
      end
        else
      begin
        if FindJsonArray(
             JsonParser.Output, JsonParser.Output.Objects[0],
             'launchSettings', LaunchSettingsArray) and
           (GetArrayLength(LaunchSettingsArray) >= 0) and
           (LaunchSettingsArray[0].Kind = JVKObject) and 
           FindJsonValue(
             JsonParser.Output,
             JsonParser.Output.Objects[LaunchSettingsArray[0].Index], 'toolDirectory',
             ToolDirectoryValue) and
           (ToolDirectoryValue.Kind = JVKString) then
        begin
          Log(Format(
            'launchSettings[0]:toolDirectory:%s', [
            JsonParser.Output.Strings[ToolDirectoryValue.Index]]));
          JsonParser.Output.Strings[ToolDirectoryValue.Index] := 'Test';
          JsonLines.Clear;
          PrintJsonParserOutput(JsonParser.Output, JsonLines);
          JsonLines.SaveToFile(FileName);
        end;
      end;
    
      ClearJsonParser(JsonParser);
      JsonLines.Free;
    end;
    

    它仍然不会保留订单(但这不重要)。


    尽管在您的情况下,您不需要解析 JSON。您可以简单地将%Selected Setup Folder% 替换为所需的值。

    Replace placeholder in an installed text file with input entered by user

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      相关资源
      最近更新 更多