If you use TJSONObject.Parse() instead, the TJSONArray will get added as a child of the object you are calling Parse() on, but it is an unnamed array so you have to retrieve the array by index:procedure TfMain.loadScenarioData(aFilename: string);var vJSONBytes: TBytes; vJSONScenario: TJSONObject; vJSONArray: TJSONArray; vJSONValue: TJSONValue; vJSONObject: TJSONObject; vJSONPair: TJSONPair; vJSONScenarioEntry: TJSONString; vJSONScenarioValue: string; vParseResult: Integer;begin vJSONBytes := TFile.ReadAllBytes(aFileName); vJSONScenario := TJSONObject.Create; try vParseResult := vJSONScenario.Parse(vJSONBytes, 0); if vParseResult >= 0 then begin //BetFair Specific 'caption' key vJSONArray := vJSONScenario.Get(0) as TJSONArray; for vJSONValue in vJSONArray do begin vJSONObject := vJSONValue as TJSONObject; vJSONPair := vJSONObject.Get('caption'); vJSONScenarioEntry := vJSONPair.JsonString; vJSONScenarioValue := vJSONScenarioEntry.Value; cbScenario.Items.Add(vJSONScenarioValue); end; end; finally vJSONScenario.Free; end;end;或者简单地:procedure TfMain.loadScenarioData(aFilename: string);var vJSONScenario: TJSONObject; vJSONValue: TJSONValue; vParseResult: Integer;begin vJSONScenario := TJSONObject.Create; try vParseResult := vJSONScenario.Parse(TFile.ReadAllBytes(aFileName), 0); if vParseResult >= 0 then begin //BetFair Specific 'caption' key for vJSONValue in vJSONScenario.Get(0) as TJSONArray do begin cbScenario.Items.Add(((vJSONValue as TJSONObject).Get('caption').JsonValue as TJSONString).Value); end; end; finally vJSONScenario.Free; end;end; 更新:如果您尝试使用 SuperObject ,则代码会更简单一些,例如:Update: If you try SuperObject instead, the code would be a little simpler, eg:procedure TfMain.loadScenarioData(aFilename: string);var vJSONScenario: ISuperObject; vJSONArray: ISuperObject; vJSONObject: ISuperObject; vJSONScenarioValue: string; I: Integer;begin vJSONScenario := TSuperObject.ParseFile(aFileName); //BetFair Specific 'caption' key vJSONArray := vJSONScenario.AsArray; for I := 0 to vJSONArray.Length-1 do begin vJSONObject := vJSONArray[I].AsObject; vJSONScenarioValue := vJSONObject.S['caption']; cbScenario.Items.Add(vJSONScenarioValue); end;end; 这篇关于使用Embarcadero代码示例使用TJSONObject解析有效JSON失败,并出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 19:40