Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

How to parse a JSON string in Inno Setup?

I have the following JSON:

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

Unfortunately TLama's Inno JSON Config library doesn't work with JSON strings but only with json files.

I tried to use JSON string instead of path to json file, but it didn't work.

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

I know I could save my JSON to a file and then parse it but it seems kind of messy.

How to parse a JSON string in Inno Setup?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use JsonParser library instead. It can parse JSON strings.

It's not as easy to use as JSONConfig.dll – but that's the reason why it is more flexible. Also it's a native Pascal Script code. So, it not only saves you from a temporary .json file, but also from a temporary .dll.

The code can be like:

[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;

Another option is to fork the Inno JSON Config library and add support for parsing strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...