isJSON static method

bool isJSON(
  1. String? str
)

验证是否为有效的 JSON 格式。

仅当输入能解析为 JSON 对象(Map)或 数组(List)时返回 true。 这是 isJSON 最常见的使用场景(校验接口返回体等结构化数据)。

一致地拒绝所有 JSON 标量:数字 / 布尔 / 字符串 / null。早期实现把 "123" 当成合法 JSON(json.decode 得到非 null 的 int),却把 "null" 当成非法(因为 json.decode("null") == null),语义不一致; 现统一为「只接受对象/数组」。

Implementation

static bool isJSON(String? str) {
  if (str == null || str.isEmpty) return false;
  try {
    final parsed = json.decode(str);
    return parsed is Map || parsed is List;
  } catch (e) {
    return false;
  }
}