In Delphi 10 Seattle, I am trying to parse a JSON that contains an array of strings in a property.
As an example, consider this:
{
"name":"Joe",
"age":45,
"languages":["c++", "java", "cobol"]
}
How do I parse languages
to obtain an array of strings?
Try something like this:
function GetLanguagesArray(const AJSON: String): TArray<String>;
var
LValue: TJSONValue;
LArray: TJSONArray;
i: Integer;
begin
Result := nil;
LValue := TJSONObject.ParseJSONValue(AJSON);
if LValue <> nil then
try
LArray := (LValue as TJSONObject).GetValue('languages') as TJSONArray;
SetLength(Result, LArray.Count);
for i := 0 to Pred(LArray.Count) do
begin
Result[i] := LArray[i].Value;
end;
finally
LValue.Free;
end;
end;