【问题标题】:Load ComboBox with Json使用 Json 加载 ComboBox
【发布时间】:2022-12-17 01:44:17
【问题描述】:

我正在尝试通过 Json 加载组合框。我设法做到了,以便在重复循环中迭代时,我在组合框中逐行添加,但我想做一些更简单的事情。是否可以提供 Combobox.Load(aString)?或不需要循环的类似东西?

我正在尝试这样做以对来自 api 的几个不同的 json 重用相同的 Combobox

【问题讨论】:

  • 如果您使用CommaText 属性,您可以一次性分配ComboBox.Items。喜欢 ComboBox1.Items.CommaText := 'One,Two,Three'; 但是这对 jsonCommaText 的转换帮助不大。

标签: delphi-7 pascal


【解决方案1】:

是否可以提供 Combobox.Load(String)?

不它不是。

或不需要循环的类似东西?

抱歉,没有。但是,您可以编写一个接受 JSON 并处理循环的函数。该函数可以:

  • 输出一个以逗号分隔的String,然后您可以将其分配给ComboBox.Items.CommaText
function ParseJSONToItems(const aJSON: string): String;
begin
  // parse JSON, set Result as needed...
end;
ComboBox1.Items.CommaText := ParseJSONToItems('{...}');
  • 输出一个TStringList然后你可以Assign()ComboBox.Items
function ParseJSONToItems(const aJSON: string): TStringList;
begin
  Result := TStringList.Create;
  try
    // parse JSON, fill Result as needed...
  except
    Result.Free;
    raise;
  end;
end;
var List := ParseJSONToItems('{...}');
try
  ComboBox1.Items.Assign(List);
finally
  List.Free;
end;
  • 输入一个目标TStrings作为输入,然后你可以传入ComboBox.Items
procedure ParseJSONToItems(const aJSON: string; aItems: TStrings);
begin
  // parse JSON, fill aItems as needed...
end;
ParseJSONToItems('{...}', ComboBox1.Items);

【讨论】:

    猜你喜欢
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多