您可以使用is
运算符:
if Assigned(JSONValue) then
begin
if JSONValue is TJSONArray then
ProcessArrayResponse(TJSONArray(JSONValue))
else if JSONValue is TJSONObject then
ProcessObjectResponse(TJSONObject(JSONValue));
end;
如果要使用case
语句,则必须创建自己的查找:
type
JsonValueType = (jsArray, jsObject, ...);
function GetJsonValueType(JSONValue: TJSONValue): JsonValueType;
begin
if JSONValue is TJSONArray then Exit(jsArray);
if JSONValue is TJSONObjct then Exit(jsObject);
...
end;
...
if Assigned(JSONValue) then
begin
case GetJsonValueType(JSONValue) of
jsArray : ProcessArrayResponse(TJSONArray(JSONValue));
jsObject : ProcessObjectResponse(TJSONObject(JSONValue));
end;
end;
或者:
type
JsonValueType = (jsArray, jsObject, ...);
var
JsonValueTypes: TDictionary<String, JsonValueType>;
...
if Assigned(JSONValue) then
begin
case JsonValueTypes[JSONValue.ClassName] of
jsArray : ProcessArrayResponse(TJSONArray(JSONValue));
jsObject : ProcessObjectResponse(TJSONObject(JSONValue));
end;
end;
...
initialization
JsonValueTypes := TDictionary<String, JsonValueType>.Create;
JsonValueTypes.Add('TSONArray', jsArray);
JsonValueTypes.Add('TSONObject', jsObject);
...
finalization
JsonValueTypes.Free;