gpt4 book ai didi

json - Inno 安装程序 : How can I edit and retrieve value from a children section of a JSON file

转载 作者:行者123 更新时间:2023-12-05 06:27:08 24 4
gpt4 key购买 nike

我正在努力创建一个安装程序,我需要编辑和检索 JSON 文件中的值。

检索和编辑 Section_2 中的值工作正常。问题是从 Section_1 的子部分编辑和检索值。下面我们可以看到一个例子:

{
"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;

非常感谢您的支持。

问候,迭戈维亚

最佳答案

我认为 JSONConfig.dll 不支持嵌套结构。

您可以使用 JsonParser library反而。它可以解析嵌套结构。尽管它不像 JSONConfig.dll 那样易于使用 – 好吧,因为它更通用。

下面的代码可以做到:

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;

代码使用了我对 How to parse a JSON string in Inno Setup? 的回答中的函数

关于json - Inno 安装程序 : How can I edit and retrieve value from a children section of a JSON file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com