如何在 Inno Setup 中解析 JSON 字符串?

4

我有以下的JSON:

{
    "Info": {
        "User": 2,
        "String": "foo"
    }
}

很遗憾,TLama的Inno JSON Config库只能处理JSON文件而非JSON字符串。

我试图使用JSON字符串代替JSON文件路径,但是它并没有起作用。

if JSONQueryInteger('{"Info":{"User":2,"String":"foo"}}', 'Info', 'User', 0, IntValue) then
    MsgBox('User=' + IntToStr(IntValue), mbInformation, MB_OK);  

我知道我可以将我的JSON保存到文件中,然后解析它,但这似乎有点凌乱。

如何在Inno Setup中解析JSON字符串?

1个回答

7
你可以使用JsonParser库来代替。它可以解析JSON字符串。
虽然它不像JSONConfig.dll那样易于使用,但这正是它更加灵活的原因。而且它是一种本地的Pascal脚本代码。因此,它不仅可以避免使用临时的.json文件,还可以避免使用临时的.dll文件。
代码可以如下所示:
[Code]

#include "JsonParser.pas"

function GetJsonRoot(Output: TJsonParserOutput): TJsonObject;
begin
  Result := Output.Objects[0];
end;

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 FindJsonObject(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Object: TJsonObject): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKObject);

  if Result then
  begin
    Object := Output.Objects[JsonValue.Index];
  end;
end;

function FindJsonNumber(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Number: TJsonNumber): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKNumber);

  if Result then
  begin
    Number := Output.Numbers[JsonValue.Index];
  end;
end;

function FindJsonString(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Str: TJsonString): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKString);
  if Result then
  begin
    Str := Output.Strings[JsonValue.Index];
  end;
end;

function ParseJsonAndLogErrors(
  var JsonParser: TJsonParser; const Source: WideString): Boolean;
var
  I: Integer;
begin
  ParseJson(JsonParser, Source);

  Result := (Length(JsonParser.Output.Errors) = 0);
  if not Result then
  begin
    Log('Error parsing JSON');
    for I := 0 to Length(JsonParser.Output.Errors) - 1 do
    begin
      Log(JsonParser.Output.Errors[I]);
    end;
  end;
end;

procedure ParseJsonString;
var
  Json: string;
  JsonParser: TJsonParser;
  I: Integer;
  JsonRoot, InfoObject: TJsonObject;
  UserNumber: TJsonNumber; // = Double
  UserString: TJsonString; // = WideString = string
begin
  Json := '{"Info":{"User":2,"String":"abc"}}';

  if ParseJsonAndLogErrors(JsonParser, Json) then
  begin
    JsonRoot := GetJsonRoot(JsonParser.Output);
    if FindJsonObject(JsonParser.Output, JsonRoot, 'Info', InfoObject) and
       FindJsonNumber(JsonParser.Output, InfoObject, 'User', UserNumber) and
       FindJsonString(JsonParser.Output, InfoObject, 'String', UserString) then
    begin
      Log(Format('Info:User:%d', [Round(UserNumber)]));
      Log(Format('Info:String:%s', [UserString]));
    end;
  end;

  ClearJsonParser(JsonParser);
end;

另一个选择是分叉Inno JSON Config库并添加解析字符串的支持。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接