{"id":2061,"date":"2023-01-25T22:41:02","date_gmt":"2023-01-25T14:41:02","guid":{"rendered":"https:\/\/courtship.top\/?p=2061"},"modified":"2023-01-25T22:41:02","modified_gmt":"2023-01-25T14:41:02","slug":"__trashed-7","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2023\/01\/25\/__trashed-7\/","title":{"rendered":"Json\u683c\u5f0f\u5b58\u50a8 Unity\u672c\u5730\u6570\u636e\u5b58\u50a8"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u77e5\u4e4e\uff1a\u6d3b\u7740\u5c31\u597d\u5566 B\u7ad9\uff1aBeaverJoe<\/p>\n\n\n\n<p>\u3010\u3010\u4e2d\u6587\u5b57\u5e55\u3011\u4f7f\u7528JSON\u65b9\u5f0f\u4fdd\u5b58\u548c\u8bfb\u53d6Unity\u6570\u636e\u4fe1\u606f\uff08\u56db\u79cd\u4fdd\u5b58\u8bfb\u53d6\u6570\u636e\u65b9\u6cd5\u7cfb\u5217\u7b2c\u4e09\u671f\uff09\u3011 https:\/\/www.bilibili.com\/video\/BV157411x7qZ\/?share_source=copy_web&amp;vd_source=1e6de1b65319e041939801e8eea80cb7<\/p>\n<cite>\u6570\u636e\u6301\u4e45\u5316-\u8ba4\u8bc6Json\u4e0eJsonUtlity(1) - \u6d3b\u7740\u5c31\u597d\u5566\u7684\u6587\u7ae0 - \u77e5\u4e4e https:\/\/zhuanlan.zhihu.com\/p\/513432977<\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Json<\/h2>\n\n\n\n<p><strong>JSON(JavaScript Object Notation)<\/strong>\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5177\u6709\u826f\u597d\u7684\u53ef\u8bfb\u548c\u4fbf\u4e8e\u5feb\u901f\u7f16\u5199\u7684\u7279\u6027\u3002\u53ef\u5728\u4e0d\u540c\u5e73\u53f0\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n\n\n\n<p>JSON\u91c7\u7528\u517c\u5bb9\u6027\u5f88\u9ad8\u7684\u3001\u5b8c\u5168\u72ec\u7acb\u4e8e\u8bed\u8a00\u6587\u672c\u683c\u5f0f\uff0c\u540c\u65f6\u4e5f\u5177\u5907\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u7684\u4e60\u60ef\u4f53\u7cfb\u7684\u884c\u4e3a\u3002\u8fd9\u4e9b\u7279\u6027\u4f7fJSON\u6210\u4e3a\u7406\u60f3\u7684\u6570\u636e\u4ea4\u6362\u8bed\u8a00\u3002<\/p>\n\n\n\n<p>JSON\u8bed\u6cd5\u662fjavaScript\u8bed\u6cd5\u7684\u5b50\u96c6\uff0cjavaScript\u7528\u3010\u3011\u4e2d\u62ec\u53f7\u6765\u8868\u793a\u6570\u7ec4\uff0c\u7528{}\u5927\u62ec\u53f7\u6765\u8868\u793a\u5bf9\u8c61\uff0cJSON\u4ea6\u662f\u5982\u6b64\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u89e3\u6790\u901f\u5ea6\u5feb\uff0c\u6570\u636e\u683c\u5f0f\u6bd4\u8f83\u7b80\u5355\uff0c\u6613\u4e8e\u8bfb\u5199\uff0c\u683c\u5f0f\u90fd\u662f\u538b\u7f29\u7684\uff0c\u5360\u7528\u5e26\u5bbd\u5c0f\u3002<\/li>\n\n\n\n<li>\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u4fbf\u4e8e\u670d\u52a1\u5668\u7aef\u7684\u89e3\u6790\u3002<\/li>\n\n\n\n<li>\u56e0\u4e3aJSON\u683c\u5f0f\u80fd\u76f4\u63a5\u4e3a\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u4f7f\u7528\uff0c\u5927\u5927\u7b80\u5316\u4e86\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u7684\u4ee3\u7801\u5f00\u53d1\u91cf\uff0c\u4e14\u5b8c\u6210\u4efb\u52a1\u4e0d\u53d8\uff0c\u5e76\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5728JSON\u91cc\u9762\uff0c\u6ce8\u91ca\u8ddf\u6b63\u5e38\u7684C#\u8bed\u8a00\u4e00\u6837\uff0c\u201c\/\/\u201d\u8868\u793a\u5355\u884c\u6ce8\u91ca\uff0c\u201c\/**\/\u201d\u8868\u793a\u591a\u884c\u6ce8\u91ca\uff0c\u56e0\u4e3aJson\u6700\u5927\u7684\u7279\u70b9\u5c31\u662f\u952e\u503c\u5bf9\uff0c\u6240\u4ee5\u6709\u4e2a\u5c0f\u77e5\u8bc6\u5c31\u662f\u4f5c<strong>\u4e3a\u503c\u7684\u503c\u7c7b\u578b\u53ef\u4ee5\u662fnull<\/strong>\uff0c\u8fd9\u4e2a\u5728JSON\u662f\u652f\u6301\u7684\uff0c\u7136\u540eJSON\u6587\u672c\u7684\u7f16\u7801\u5fc5\u987b\u662fUTF-8\u5426\u5219\u662f\u4f1a\u62a5\u9519\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">JsonUtility<\/h2>\n\n\n\n<p>JsonUtlity\u662fUnity\u81ea\u5e26\u7684\u7528\u4e8e\u89e3\u6790Json\u7684\u516c\u5171\u7c7b\uff0c\u5b83\u53ef\u4ee5\u5c06\u5185\u5b58\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u80fd\u5c06Json\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u4e3a\u7c7b\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u65b0\u5efa\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class XiaoXiaoZe\n    {\n        public string name;\n        public int age;\n        public bool sex;\n        public float testFloat;\n        public double testDouble;\n\n        public int&#91;] ids;\n        public List&lt;int&gt; ids2;\n        public Dictionary&lt;int, string&gt; dic;\n        public Dictionary&lt;string, string&gt; dic2;\n\n        \/\/ \u5bf9\u4e8e\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\uff0c\u6211\u4eec\u5728\u5bf9\u5b83\u4eec\u8fdb\u884c\u5e8f\u5217\u5316\u4e4b\u524d\u9700\u8981\u5728\u7c7b\u7684\u524d\u9762\u52a0\u4e0a&#91;System.Serializable]\u7279\u6027\n        public Human human;\n        public List&lt;Human&gt; humans;\n\n        &#91;SerializeField]\/\/ \u60f3\u8981\u5e8f\u5217\u5316\u79c1\u6709\u53d8\u91cf \u9700\u8981\u52a0\u4e0a\u7279\u6027&#91;SerializeField]\n        private int privateI = 1;\n        &#91;SerializeField]\/\/ \u60f3\u8981\u5e8f\u5217\u5316\u79c1\u6709\u53d8\u91cf \u9700\u8981\u52a0\u4e0a\u7279\u6027&#91;SerializeField]\n        protected int protectedI = 2;\n    }\n\n    &#91;System.Serializable]\n    public class Human\n    {\n        public int age;\n        public string name;\n\n        public Human(int age, string name)\n        {\n            this.age = age;\n            this.name = name;\n        }\n    }\n\nprivate void Start()\n    {\n        \/\/ 3.\u4f7f\u7528JsonUtlity\u8fdb\u884c\u5e8f\u5217\u5316(\u5e8f\u5217\u5316\uff1a\u628a\u5185\u5b58\u6570\u636e\u5b58\u50a8\u5230\u786c\u76d8\u4e0a)\n        XiaoXiaoZe xxz = new XiaoXiaoZe();\n        xxz.name = \"\u5c0f\u5c0f\u6cfd\";\n        xxz.age = 999;\n        xxz.sex = true;\n        xxz.testFloat = 1.4f;\n        xxz.testDouble = 1.4;\n\n        xxz.ids = new&#91;] { 1, 2, 3, 4 };\n        xxz.ids2 = new List&lt;int&gt;() { 1, 2, 3 };\n        xxz.dic = new Dictionary&lt;int, string&gt;() { { 1, \"123\" }, { 2, \"234\" } };\n        xxz.dic2 = new Dictionary&lt;string, string&gt;() { { \"1\", \"123\" }, { \"2\", \"234\" } };\n\n        \/\/ \u5bf9\u4e8e\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\uff0c\u6211\u4eec\u5728\u5bf9\u5b83\u4eec\u8fdb\u884c\u5e8f\u5217\u5316\u4e4b\u524d\u9700\u8981\u5728\u7c7b\u7684\u524d\u9762\u52a0\u4e0a&#91;System.Serializable]\u7279\u6027\n        xxz.human = null;\/\/ \u8fd9\u91cc\u662f\u4e3a\u4e86\u6d4b\u8bd5NUll\u7c7b\u578b\n        xxz.humans = new List&lt;Human&gt;() { new Human(2, \"\u5c0f\u660e\"), new Human(3, \"\u5c0f\u5f3a\") };\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3ajson<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/ JsonUtility\u63d0\u4f9b\u4e86\u73b0\u6210\u7684\u65b9\u6cd5\u53ef\u4ee5\u628a\u7c7b\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJson\u5b57\u7b26\u4e32\n<\/em>string jsonStr = JsonUtility.<em>ToJson<\/em>(xxz);\n\n\/\/\u5199\u5165\u6587\u4ef6\nFile.<em>WriteAllText<\/em>(Application.<strong>persistentDataPath <\/strong>+ \"\/XXZ.json\", jsonStr);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ece\u6587\u4ef6\u6570\u636e\u8bfb\u51fa<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4f7f\u7528JsonUtlity\u8fdb\u884c\u53cd\u5e8f\u5217\u5316(\u628a\u786c\u76d8\u6570\u636e\u5b58\u50a8\u5230\u5185\u5b58\u91cc)\ntext = File.ReadAllText(Application.persistentDataPath + \"\/XXZ.json\");\n\u200b\n\/\/ \u7b2c\u4e00\u79cd\u5199\u6cd5\nXiaoXiaoZe xxz2 = JsonUtility.FromJson(text, typeof(XiaoXiaoZe)) as XiaoXiaoZe;\n\/\/ \u7b2c\u4e8c\u79cd\u5199\u6cd5\nxxz2 = JsonUtility.FromJson&lt;XiaoXiaoZe&gt;(text);\nprint(xxz2.name);\nprint(xxz2.age);\nprint(xxz2.sex);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6ce8\u610f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b57\u5178\u662f\u65e0\u6cd5\u652f\u6301\u5e8f\u5217\u5316\u7684 <\/li>\n\n\n\n<li>Float\u7c7b\u578b\u51fa\u73b0\u4e86\u5f88\u591a\u4f4d\u6570\u7684\u5c0f\u6570\u90e8\u5206(\u5176\u5b9e\u8fd9\u4e2a\u6ca1\u6709\u4ec0\u4e48\u5927\u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u6700\u7ec8\u53cd\u5e8f\u5217\u5316\u7684\u7ed3\u679c\u662f\u6b63\u786e\u7684\u5c31\u884c) <\/li>\n\n\n\n<li>\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u7c7b\u5373\u4f7f\u662fnull\uff0c\u5e8f\u5217\u5316\u4e4b\u540e\u4e5f\u4f1a\u5b58\u50a8\u81ea\u5b9a\u4e49\u7c7b\u5185\u90e8\u5b57\u6bb5\u7c7b\u578b\u7684\u9ed8\u8ba4\u503c(\u8ddfxml\u4e0d\u4e00\u6837\uff0cxml\u662f\u4e0d\u4f1a\u5b58\u50a8) <\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u7c7b\u9700\u8981\u52a0\u4e0a\u5e8f\u5217\u5316\u7279\u6027[System.Serializable]<\/li>\n\n\n\n<li> \u60f3\u8981\u5e8f\u5217\u5316\u79c1\u6709\u53d8\u91cf \u9700\u8981\u52a0\u4e0a\u7279\u6027[SerializeField]<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u77e5\u4e4e\uff1a\u6d3b\u7740\u5c31\u597d\u5566 B\u7ad9\uff1aBeaverJoe \u3010\u3010\u4e2d\u6587\u5b57\u5e55\u3011\u4f7f\u7528JSON\u65b9\u5f0f\u4fdd\u5b58\u548c\u8bfb\u53d6Unity\u6570\u636e\u4fe1\u606f\uff08\u56db\u79cd\u4fdd\u5b58\u8bfb\u53d6\u6570\u636e\u65b9\u6cd5\u7cfb\u5217\u7b2c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-2061","post","type-post","status-publish","format-standard","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2061"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2061\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}