2016-10-03 104 views
2

我有这样的JSON数据:获取JSON数组和项目添加到组合框在Delphi

[ 
    { 
    "Name":"val1", 
    "Age":"25" 
    }, 
    { 
    "Name":"Vtya", 
    "Age":"24" 
    }, 
    { 
    "Name":"fgani", 
    "Age":"21" 
    }, 
    { 
    "Name":"Shami", 
    "Age":"21" 
    }, 
    { 
    "Name":"Slakf", 
    "Age":"22" 
    } 
] 

我写了这个代码来解析数据和Name值添加到组合框:

procedure TJSON_Sample.FormCreate(Sender: TObject); 
var 
    LJsonArray: TJSONArray; 
    LJsonValue, LITEM: TJSONValue; 
    lJsonData: string; 
    ljsPair: TJsonPair; 
begin 
    LJsonArray := TJSONObject.ParseJSONValue(TEncoding. 
    Default.GetBytes(lJsonData), 0) as TJSONArray;//lJsonData contains the above mentioned JSON data 
    try 
    for LJsonValue in LJsonArray do 
    begin 
     for LITEM in TJSONArray(LJsonValue) do 
     begin 
     cmbBox_Name.Items.Add(TJsonPair(LITEM).JsonValue.Value); 
     end; 
    end; 
    finally 
    LJsonArray.Free; 
    end; 
end; 

当我运行它时,它将所有名称和年龄添加到Combobox中。有人可以帮助我添加名称吗?

+0

我还想说,您使用未经检查的强制转换意味着如果您的代码遇到具有不正确形式的数据,其行为将以未定义的方式运行。您需要使用is和as运算符进行检查类型测试和转换。 –

回答

1

这解决了我的请求

procedure TJSON_Sample.FormCreate(Sender: TObject); 
var 
    LITEM, lJsonValue: TJSONValue; 
    lJsonData: string; 
begin 
    lJsonValue := TJSONObject.ParseJSONValue(TEncoding. 
    Default.GetBytes(lJsonData), 0);//lJsonData contains the above mentioned JSON data 
    if lJsonValue <> nil then 
    try 
     begin 
     for LITEM in lJsonValue as TJSONArray do 
     begin 
      cmbBox_Name.Items.Add(((LITEM as TJSONObject).Get('Name') .JsonValue as TJSONString).Value); 
     end; 
     end; 
    finally 
     lJsonValue.Free; 
    end; 
end; 
0

我会考虑使用Items.AddObject方法,并在组合框的OnChange事件上使用Items.Object[ComboBox1.ItemIndex]

2

您的代码通过JSON循环良好。您的问题是在将项目添加到组合框时仅获取“名称”值。

尝试GetValue('Name')而不是整个JSONValue.Value。

procedure TJSON_Sample.FormCreate(Sender: TObject); 
var 
    LJsonArray: TJSONArray; 
    LJsonValue, LITEM: TJSONValue; 
    lJsonData: string; 
    ljsPair: TJsonPair; 
begin 
    LJsonArray := TJSONObject.ParseJSONValue(TEncoding. 
    Default.GetBytes(lJsonData), 0) as TJSONArray;//lJsonData contains the above mentioned JSON data 
    try 
    for LJsonValue in LJsonArray do 
    begin 
     for LITEM in TJSONArray(LJsonValue) do 
     begin 
     cmbBox_Name.Items.Add(TJsonObject(LITEM).GetValue('Name').Tostring); 
     end; 
    end; 
    finally 
    LJsonArray.Free; 
    end; 
end; 
+1

'cmbBox_Name.Items.Add(TJsonObject(LITEM).GetValue('Name')。Tostring);'此语句获取名称列表并添加到ComboBox,但双引号仍然保留,并且该语句应该被添加在末尾使用'.Tostring',只有代码执行,否则会产生无效的类型错误。 – userhi

+0

确实,我是从记忆中写下来的。固定。 –

0

这里几乎是相同的代码,但我想补充BeginUpdate/EndUpdate的组合框项目和使用通用方法来避免类型转换。

procedure TJSON_Sample.FormCreate(Sender: TObject); 
var 
    LJson, LItem: TJSONValue; 
    lJsonData: string; 
begin 
    cmbBox_Name.Items.BeginUpdate; 
    try 
    cmbBox_Name.Items.Clear; 

    LJson := TJSONObject.ParseJSONValue(TEncoding. 
     Default.GetBytes(lJsonData), 0);//lJsonData contains the above mentioned JSON data 

    if Assigned(LJson) then 
    begin 
     for LItem in LJson as TJSONArray do 
     cmbBox_Name.Items.Add(LItem.GetValue<string>('Name')); 
    end; 
    finally 
    cmbBox_Name.Items.EndUpdate; 
    end; 
end;