{"id":342,"date":"2023-02-25T06:59:04","date_gmt":"2023-02-24T22:59:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=342"},"modified":"2023-04-30T14:48:19","modified_gmt":"2023-04-30T06:48:19","slug":"android-studio-add-gson-and-use-gsonformat-to-quickly-implement-entity-classes","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-studio-add-gson-and-use-gsonformat-to-quickly-implement-entity-classes\/","title":{"rendered":"Android Studio\u4e2d\u6dfb\u52a0GSON\u5e76\u4f7f\u7528GsonFormat\u5feb\u901f\u5b9e\u73b0\u5b9e\u4f53\u7c7b"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>GSON\u662fGoogle\u5f00\u53d1\u7684Java API\uff0c\u7528\u4e8e\u8f6c\u6362Java\u5bf9\u8c61\u548cJson\u5bf9\u8c61\u3002<\/p>\n<p>GitHub\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/google\/gson\">https:\/\/github.com\/google\/gson<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>\u6dfb\u52a0GSON\u4f9d\u8d56<\/h2>\n<h3>\u65b9\u6cd5\u4e00<\/h3>\n<ul>\n<li>1\u3001\u70b9\u51fb\u83dc\u5355 File -&gt; Project Structure\uff0c\u7136\u540e\u70b9\u51fb+\u53f7\uff0c\u9009\u62e9Library dependency<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u4f9d\u8d56\u5e93.png\" alt=\"\u4f9d\u8d56\u5e93\" \/><\/p>\n<ul>\n<li>2\u3001\u641c\u7d22GSON\uff0c\u9009\u62e9\u6dfb\u52a0<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u641c\u7d22gson.png\" alt=\"\u641c\u7d22gson\" \/><\/p>\n<pre><code>com.google.code.gson:gson:2.8.0\ncom.google.code.gson:gson-parent:2.8.0<\/code><\/pre>\n<h3>\u65b9\u6cd5\u4e8c<\/h3>\n<p>\u5728app\u5c42\u7ea7\u7684build.gradle\u914d\u7f6e<\/p>\n<pre><code>compile &#039;com.google.code.gson:gson:2.8.0&#039;\ncompile &#039;com.google.code.gson:gson-parent:2.8.0&#039;<\/code><\/pre>\n<h2>\u5b89\u88c5GsonFormat<\/h2>\n<p>\u70b9\u51fb\u83dc\u5355 File -&gt; Settings\uff0c\u9009\u62e9 Plugins<\/p>\n<p>\u641c\u7d22 GsonFormat\u63d2\u4ef6\u5e76\u6267\u884c\u5b89\u88c5\uff0c\u5b89\u88c5\u5b8c\u6210\u540e\u91cd\u542fAndroid Studio<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u5b89\u88c5GsonFormat.png\" alt=\"\u5b89\u88c5GsonFormat\" \/><\/p>\n<h2>\u4f7f\u7528GsonFormat<\/h2>\n<p>\u5728\u4ee3\u7801\u4e0a\u65b9\u53f3\u952e Generate -&gt; GsonFormat<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u53f3\u952eGenerate.png\" alt=\"\u53f3\u952eGenerate\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u9009\u62e9GsonFormat.png\" alt=\"\u9009\u62e9GsonFormat\" \/><\/p>\n<p>\u4f20\u5165JSON\u6837\u4f8b\u6570\u636e<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u4f20\u5165JSON\u6570\u636e.png\" alt=\"\u4f20\u5165JSON\u6570\u636e\" \/><\/p>\n<p>\u9009\u62e9\u57df<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u9009\u62e9\u57df.png\" alt=\"\u9009\u62e9\u57df\" \/><\/p>\n<p>\u81ea\u52a8\u751f\u6210JavaBean<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u81ea\u52a8\u751f\u6210JavaBean.png\" alt=\"\u81ea\u52a8\u751f\u6210JavaBean\" \/><\/p>\n<h2>Gson \u4f7f\u7528<\/h2>\n<p>\u89e3\u6790\uff1a<\/p>\n<pre><code class=\"language-java\">String result = &quot;{&#039;head_url&#039;: &#039;http:\/\/www.androidios.cc\/images\/avatar.png&#039;, &#039;id&#039;: 1, &#039;username&#039;: &#039;Joe.Ye&#039;}&quot;;\nGson gson = new Gson();\nUser user = gson.fromJson(result, User.class);\nLog.i(TAG, user.getUsername());<\/code><\/pre>\n<p>\u5c01\u88c5\uff1a<\/p>\n<pre><code class=\"language-java\">User user = new User(1, &quot;Joe.Ye&quot;)\nString json = new Gson().toJson(user);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb GSON\u662fGoogle\u5f00\u53d1\u7684Java API\uff0c\u7528\u4e8e\u8f6c\u6362Java\u5bf9\u8c61\u548cJson\u5bf9\u8c61\u3002 GitHub\uff1ahtt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[118],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-gson"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/342","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=342"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}