问题
I'm working to create an installer and I need to edit and retrieve values from the JSON file.
To retrieve and edit the values from the Section_2
works fine. The problem is to edit and retrieve values from the children sections of Section_1
. Bellow we can see an example:
{
"Section_1": {
"children_1": {
"children_1_1": "value_1",
"children_1_2": "value_2"
},
"children_2": "blablabla"
},
"Section_2": {
"children_2_1": "value_1",
"children_2_2": "value_2"
}
}
[Files]
Source: "{#ProjectUrl}\JSONConfig.dll"; Flags: dontcopy
[Code]
var
FileName: WideString;
StrValue: WideString;
StrLength: Integer;
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';
function editAppSettingsJson(Section_1: String; Section_2:String): Boolean;
begin
FileName := '{#AppSettingsJsonFile}';
SetLength(StrValue, 16);
StrLength := Length(StrValue);
Result := True;
{ Does not work. How can I edit it? }
if not JSONWriteString(FileName, 'children_1', 'children_1_1',
Section_1) then
begin
MsgBox('JSONWriteString Section_1:children_1:children_1_1 failed!',
mbError, MB_OK);
Result := False;
end;
{ Works fine. }
if not JSONWriteString(FileName, 'Section_2', 'children_2_1', Section_2)
then
begin
MsgBox('JSONWriteString Section_2:children_2_1 failed!', mbError,
MB_OK);
Result := False;
end;
end;
procedure InitializeWizard;
var
value_1: String;
value_2: String;
begin
value_1:= 'value_2';
value_2:= 'value_3';
editAppSettingsJson(value_1, value_2);
end;
In advance thank you very much for your support.
Regards, Diego Via
回答1:
I do not think that JSONConfig.dll
supports nested structures.
You can use JsonParser library instead. It can parse nested structures. Though it's not as easy to use as JSONConfig.dll
– well, because it's more versatile.
The following code will do:
var
JsonLines: TStringList;
JsonParser: TJsonParser;
JsonRoot, Section1Object, Children1Object: TJsonObject;
Child11Value: TJsonValue;
begin
JsonLines := TStringList.Create;
JsonLines.LoadFromFile(FileName);
if ParseJsonAndLogErrors(JsonParser, JsonLines.Text) then
begin
JsonRoot := GetJsonRoot(JsonParser.Output);
if FindJsonObject(JsonParser.Output, JsonRoot, 'Section_1', Section1Object) and
FindJsonObject(JsonParser.Output, Section1Object, 'children_1', Children1Object) and
FindJsonValue(JsonParser.Output, Children1Object, 'children_1_1', Child11Value) and
(Child11Value.Kind = JVKString) then
begin
Log(Format('children_1_1 previous value %s', [
JsonParser.Output.Strings[Child11Value.Index]]));
JsonParser.Output.Strings[Child11Value.Index] := 'new value';
JsonLines.Clear;
PrintJsonParserOutput(JsonParser.Output, JsonLines);
JsonLines.SaveToFile(FileName);
end;
end;
end;
The code uses functions from my answer to How to parse a JSON string in Inno Setup?
来源:https://stackoverflow.com/questions/55520447/inno-setup-how-can-i-edit-and-retrieve-value-from-a-children-section-of-a-json