百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jsonlint

> 编程语言
开源

PHP 的 JSON Lint

1.3K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

PHP 的 JSON Lint

JSON Lint =========== 使用方法 ----- php use Seld\JsonLint\JsonParser; $parser = new JsonParser(); // 如果 JSON 有效,则返回 null,否则返回 ParsingException 对象。 $parser-lint($json); // 请调用异常对象上的 getMessage() 方法,以获取 // 格式化的错误消息,如下所示 Parse error on line 2: // ... "key": "value" "numbers": [1, 2, 3] // ----------------------^ // 期望为 'EOF', '}', ':', ',', ']' // 请调用异常上的 getDetails() 方法,以获取更多信息。 // 返回解析的 JSON,与 jsondecode() 类似,但速度较慢,在失败时会抛出异常。 $parser-parse($json); 您还可以向 JsonParser::lint/parse 添加额外的标志,以调整功能: - JsonParser::DETECTKEYCONFLICTS 在重复的键上抛出异常。 - JsonParser::ALLOWDUPLICATEKEYS 将收集重复的键。例如,如果您有两个 foo 键,它们将分别成为 foo 和 foo.2。 - JsonParser::PARSETOASSOC 将解析为关联数组,而不是 stdClass 对象。 - JsonParser::ALLOWCOMMENTS 解析时允许 (并忽略) JSON 文档中的 inline // 和 multiline // 注释。 - JsonParser::ALLOWDUPLICATEKEYSTOARRAY 将收集重复的键。例如,如果您有两个 foo 键,foo 键将成为一个对象 (或关联模式下的数组),所有 foo 值都可通过 $result-foo-duplicates (或关联模式下的 $result['foo']['duplicates']) 访问。 - JsonParser::VALIDATEUTF8ENCODING 在解析之前验证整个输入是否符合 RFC 3629 定义的 UTF-8 格式,否则将抛出 InvalidEncodingException,指向第一个无效字节。示例: php $parser = new JsonParser; try { $parser-parse(filegetcontents($jsonFile), JsonParser::DETECTKEYCONFLICTS); } catch (DuplicateKeyException $e) { $details = $e-getDetails(); echo 'Key '.$details['key'].' is a duplicate in '.$jsonFile.' at line '.$details['line']; } 验证 UTF-8 编码 ------------------------- 使用 VALIDATEUTF8ENCODING 标志后面的 UTF-8 验证也可通过 Seld\JsonLint\Utf8Validator 类作为独立的可重用实用程序来使用,如果您需要在 JSON 解析之外验证 UTF-8 编码: php use Seld\JsonLint\Utf8Validator; use Seld\JsonLint\InvalidEncodi…

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •JsonParser::DETECT_KEY_CONFLICTS throws an exception on duplicate keys.
  • •JsonParser::ALLOW_DUPLICATE_KEYS collects duplicate keys. e.g. if you have two foo keys they will end up as foo and foo.2.
  • •JsonParser::PARSE_TO_ASSOC parses to associative arrays instead of stdClass objects.
  • •JsonParser::ALLOW_COMMENTS parses while allowing (and ignoring) inline // and multiline /* */ comments in the JSON document.
  • •PHP 5.3+
  • •[optional] PHPUnit 3.5+ to execute the test suite (phpunit --version)

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言