{"id":1792,"date":"2023-03-27T22:34:54","date_gmt":"2023-03-27T14:34:54","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1792"},"modified":"2023-04-23T21:14:01","modified_gmt":"2023-04-23T13:14:01","slug":"fastjson-verify-if-json-string-is-legal","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/27\/fastjson-verify-if-json-string-is-legal\/","title":{"rendered":"FastJson\u6821\u9a8cjson\u5b57\u7b26\u4e32\u662f\u5426\u5408\u6cd5"},"content":{"rendered":"<p><code>JSON.isValid(String str)<\/code>\u3001<code>JSON.isValidObject(String str)<\/code>\u3001<code>JSON.isValidArray(String str)<\/code>\u5747\u5df2\u8fc7\u65f6\u5e76\u5b58\u5728Bug\uff0cFastJson 1.2.60\u65b0\u589e\u52a0\u4e00\u4e2a\u9ad8\u6027\u80fd\u6821\u9a8cJSON\u5b57\u7b26\u4e32\u7684API\uff0c\u8fd8\u5728\u9a8c\u8bc1\u9636\u6bb5\u3002\u652f\u6301\u8f93\u5165\u7c7b\u578b\u662f\u5b57\u7b26\u4e32\u3001<code>byte<\/code>\u6570\u7ec4\u3001<code>InputStream<\/code>\u3001<code>Reader<\/code>\u3002<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/alibaba\/fastjson\/wiki\/JSONValidator\">https:\/\/github.com\/alibaba\/fastjson\/wiki\/JSONValidator<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>JSONValidator API<\/h2>\n<pre><code class=\"language-java\">package com.alibaba.fastjson;\n\npublic abstract class JSONValidator {\n    \/\/ \u652f\u6301\u8f93\u5165\u7c7b\u578b\u662futf8\u7f16\u7801\u7684byte[]\u3001String\u3001InputStream\u3001Reader\n    public static JSONValidator fromUtf8(byte[] jsonBytes)\n    public static JSONValidator fromUtf8(InputStream is)\n    public static JSONValidator from(String str)\n    public static JSONValidator from(Reader r)\n\n    public boolean validate();\n}<\/code><\/pre>\n<h2>JSON\u5b57\u7b26\u4e32\u6821\u9a8c<\/h2>\n<pre><code class=\"language-java\">String jsonStr = ...\nJSONValidator validator = JSONValidator.from(jsonStr);\nboolean valid = validator.validate();<\/code><\/pre>\n<h2>UTF8 byte\u6570\u7ec4\u6821\u9a8c<\/h2>\n<p>\u8f93\u5165\u53c2\u6570\u662f<code>byte[]<\/code>\u65f6\uff0c\u53ea\u652f\u6301UTF8\u7f16\u7801<\/p>\n<pre><code class=\"language-java\">byte[] jsonBytes = ...\nJSONValidator validator = JSONValidator.fromUtf8(jsonBytes);\nboolean valid = validator.validate();<\/code><\/pre>\n<h2>InputStream\u6821\u9a8c<\/h2>\n<p>\u8f93\u5165\u53c2\u6570\u662f<code>InputSteam<\/code>\u65f6\uff0c\u53ea\u652f\u6301UTF8\u7f16\u7801<\/p>\n<pre><code class=\"language-java\">InputStream is = ...\nJSONValidator validator = JSONValidator.fromUtf8(is);\nboolean valid = validator.validate();<\/code><\/pre>\n<h2>Reader\u6821\u9a8c<\/h2>\n<pre><code class=\"language-java\">Reader r = ...\nJSONValidator validator = JSONValidator.from(r);\nboolean valid = validator.validate();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSON.isValid(String str)\u3001JSON.isValidObject(String str) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[58],"class_list":["post-1792","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-fastjson"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1792"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1792\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}