【问题标题】:Delphi JSONValue get valueDelphi JSONValue 获取值
【发布时间】:2025-12-22 18:40:11
【问题描述】:

我的代码已经到了这里:

JSONArray := TJSONObject.ParseJSONValue(Text) as TJSONArray;

for var JSONValue in JSONArray do
  begin
    ListBox1.Items.Add(JSONValue.Value);
  end;

请注意Text := '[{"jahre":2},{"jahre":4},{"jahre":15}]' 是有效的 JSON 格式。如何在列表中获取 2019 年和 2018 年的项目?

使用上面的代码,我在列表框中得到白色项目。

【问题讨论】:

    标签: delphi


    【解决方案1】:

    每个JSONValue 都是数组的一个“片段”,每个片段都是一个对象。您必须将类型转换为TJSONObject,然后才能获取值。

    ListBox1.Items.Add((JSONValue as TJSONObject).GetValue('jahre').ToString);
    

    更多信息可以在in the doc 找到(如果您使用的是 10.3,则 JSON 库已得到改进)

    【讨论】: