2017-12-14 40 views
1

Inno Setupの助けを借りてインストーラを作成したいと思います。すべてのコンピュータでプログラムを動作させるには、.json -fileのツールディレクトリを変更する必要があります。私はinno-json-configライブラリを使用することによってこの問題を解決することを望んだ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" 
    } 
    ], 
} 

:ここでは、このファイルからの抜粋です。残念ながら、コードを実行すると、行が反転し(最後の行が最初に来る)、変更は行われませんでした。

[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 '[email protected]:jsonconfig.dll stdcall'; 
function JSONWriteString(FileName, Section, Key, 
    Value: WideString): Boolean; 
    external '[email protected]: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; 

ご協力いただきありがとうございます。

よろしく、

アレックス

答えて

0

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を参照してください。

関連する問題