我与 Inno JSON Config 一起工作(如 Pascal Script fails to retrieve wide string from a custom DLL )读取名为 items.json
的简单 JSON 文件:
{
"Item_0":{
"Key_1": "String 1",
"Key_2": "1",
"Key_3": "True"
},
"Item_1":{
"Key_1": "String 2",
"Key_2": "2",
"Key_3": "False"
}
}
这是我的代码:
[Files]
Source: "JSONConfig.dll"; DestDir: "{tmp}\src"; Flags: dontcopy;
Source: "json_lists\*"; DestDir: "{tmp}\json_lists"; Flags: dontcopy;
[Code]
var
ItemList: array of WideString;
function JSONQueryString(FileName, Section, Key, Default: WideString;
var Value: WideString; var ValueLength: Integer): Boolean;
external 'JSONQueryString@files:jsonconfig.dll stdcall';
procedure AppendItem(var ItemArray: array of WideString; Item: WideString);
begin
SetLength(ItemArray, GetArrayLength(ItemArray) + 1);
ItemArray[GetArrayLength(ItemArray) - 1] := Item;
end;
function ParseJson(Filename: WideString; Item: String; Key: String; var ItemArray: array of WideString): Boolean;
var
StrValue: WideString;
StrLength: Integer;
ItemNumber: Integer;
Path: WideString;
begin
SetLength(StrValue, 32);
StrLength := Length(StrValue);
result := False;
Path := ExpandConstant('{tmp}'+ '\json_lists\' + Filename);
ItemNumber := 0;
while JSONQueryString(Path, ExpandConstant(Item + '_' + IntToStr(ItemNumber)), Key, 'Default', StrValue, StrLength) do
begin
ItemNumber := ItemNumber + 1;
AppendItem(ItemArray, StrValue);
result := True;
end;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
I: Integer;
PathForNextFile: WideString;
begin
if CurPageID = wpWelcome then
begin
if GetArrayLength(ItemList) = 0 then begin
ExtractTemporaryFiles('{tmp}\json_lists\*');
if ParseJson('items.json','Item', 'Key_1', ItemList) then begin
for I := 0 to GetArrayLength(ItemList) - 1 do begin
PathForNextFile := ItemList[I] + '.json';
Log(PathForNextFile); //**
end;
end else begin
Log('Error while parsing')
end;
end;
end;
result := True;
end;
我在代码中的 //**
处遇到两个问题(行 Log(PathForNextFile);
):
- 日志仅提示“String”,不带后面的数字(一般为0或1);
- 日志不会提示后缀“.json”。这是很有问题的,因为我需要带有后缀“.json”的变量
PathForNextFile
来解析另一个文件。
我不知道为什么会发生这种情况,欢迎任何帮助!
理想情况下,我只想处理 String
而不是 WideString
,所以我应该编写一个 PowerShell 脚本,该脚本在 Inno Setup 中调用时返回正确的信息作为 String
,如果可能的话?
我尝试使用 koldev 的 JsonParser Library但我无法弄清楚它是如何工作的,即使使用 How to parse a JSON string in Inno Setup?因此,再次欢迎任何帮助!
最佳答案
甚至来自 Inno Setup: Working with JSON 的原始代码返回被截断的值。 comment there by @yuval from 2015 (!) 说:
Also JSONQueryString never retrieves the last character
我建议您使用JsonParser library .
以下是使用 JsonParser 库实现的代码的等效项(以及我来自 How to parse a JSON string in Inno Setup? 的便捷函数):
function ParseJsonFile(
Filename: string; Item: string; Key: string; var ItemArray: array of string):
Boolean;
var
JsonLines: TStringList;
JsonParser: TJsonParser;
ItemNumber: Integer;
JsonRoot, ItemObject: TJsonObject;
StrValue: TJsonString; // = WideString = string
ItemKey: string;
begin
JsonLines := TStringList.Create;
JsonLines.LoadFromFile(Filename);
Result := False;
if ParseJsonAndLogErrors(JsonParser, JsonLines.Text) then
begin
JsonRoot := GetJsonRoot(JsonParser.Output);
ItemNumber := 0;
while True do
begin
ItemKey := Format('%s_%d', [Item, ItemNumber]);
if FindJsonObject(JsonParser.Output, JsonRoot, ItemKey, ItemObject) and
FindJsonString(JsonParser.Output, ItemObject, Key, StrValue) then
begin
Inc(ItemNumber);
SetLength(ItemArray, GetArrayLength(ItemArray) + 1);
ItemArray[GetArrayLength(ItemArray) - 1] := StrValue;
Result := True;
end
else Break;
end;
end;
ClearJsonParser(JsonParser);
end;
关于json - 从 Inno Setup 中的 JSON 文件的每个对象中读取特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76043047/