解析来自 ipregistry 的响应时,在 Inno Setup 中检查 JSON 对象中的所有值

问题描述

我正在尝试使用 ipregistry IP 地理定位和威胁检测 API 来阻止机器人安装。但我得到以下格式的结果。如果以下任何一项是 Map<String,Optional<Foo>>,您能帮我将它与 Inno Setup 集成以退出安装程序吗?

API 网址 https://api.ipregistry.co/50.75.90.218?key=roygrcxz372rjb&fields=security

API 结果

true

谢谢

解决方法

您已经知道如何调用在线API:
Check an IP address against an online list in Inno Setup

您基本上已经知道如何解析 JSON,但是好吧,这有点具体。使用 JsonParser library 和我来自 How to parse a JSON string in Inno Setup? 的函数,您可以编写:

function AllowInstallation(Json: string): Boolean;
var
  JsonParser: TJsonParser;
  JsonRoot,SecurityObject: TJsonObject;
  I: Integer;
begin
  Result := True;
  Log('Parsing');
  if not ParseJsonAndLogErrors(JsonParser,Json) then
  begin
    Log('Cannot parse,allowing installation');
  end
    else
  begin
    Log('Parsed');
    JsonRoot := GetJsonRoot(JsonParser.Output);
    if not FindJsonObject(JsonParser.Output,JsonRoot,'security',SecurityObject) then
    begin
      Log('Cannot find "security",allowing installation');
    end
      else
    begin
      for I := 0 to Length(SecurityObject) - 1 do
      begin
        if SecurityObject[I].Value.Kind <> JVKWord then
        begin
          Log(Format('"%s" is not "word",ignoring',[SecurityObject[I].Key]));
        end
          else
        if JsonParser.Output.Words[SecurityObject[I].Value.Index] = JWTrue then
        begin
          Log(Format('"%s" is "true",not allowing the installation',[
            SecurityObject[I].Key]));
          Result := False;
        end
          else
        begin
          Log(Format('"%s" is not "true"',[SecurityObject[I].Key]));
        end;
      end;
    end;
  end;
  ClearJsonParser(JsonParser);
end;