{"id":1707,"date":"2017-02-17T17:31:04","date_gmt":"2017-02-17T09:31:04","guid":{"rendered":"https:\/\/cowmanchiang.me\/wp\/?p=1707"},"modified":"2023-10-31T15:42:48","modified_gmt":"2023-10-31T07:42:48","slug":"gson-usage-formjson","status":"publish","type":"post","link":"https:\/\/cowmanchiang.me\/wp\/?p=1707","title":{"rendered":"Gson usage : formJson"},"content":{"rendered":"<p>avoid to face the exception &#8216;java.lang.ClassCastException With com.google.gson.internal.LinkedTreeMap cannot be cast to&#8230;&#8217;, we use the TypeToken to convert json to List<\/p>\n<p><object width=\"300\" height=\"150\"><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-title=\"\">public class Object {\n    @SerializedName(&quot;n&quot;)\n    private String name;\n    @SerializedName(&quot;i&quot;)\n    private String id;\n\n    public String getName() {\n        return name;\n    }\n\n    public Object setName(String name) {\n        this.name = name;\n        return this;\n    }\n\n    public String getId() {\n        return id;\n    }\n\n    public Object setId(String id) {\n        this.id = id;\n        return this;\n    }\n}\n\n\/\/ [{&quot;n&quot;:&quot;cowman&quot;,&quot;i&quot;:&quot;cowman@xxx.com.tw&quot;}, {&quot;n&quot;:&quot;cowman1&quot;,&quot;i&quot;:&quot;cowman1@xxx.com.tw&quot;}]\n\nList&lt;Object&gt; ObjectList = new Gson().fromJson(targets, \nnew TypeToken&lt;List&lt;Object&gt;&gt;(){}.getType());<\/pre>\n<p><\/object><\/p>\n","protected":false},"excerpt":{"rendered":"<p>avoid to face the exception &#8216;java.lang.ClassCastException With com.google.gson.internal.LinkedTreeMap cannot be cast to&#8230;&#8217;, we use the TypeToken to convert json to List public class Object { @SerializedName(&quot;n&quot;) private String name; @SerializedName(&quot;i&quot;) private String id; public String getName() { return name; } &hellip; <a href=\"https:\/\/cowmanchiang.me\/wp\/?p=1707\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-1707","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1707"}],"version-history":[{"count":5,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1707\/revisions"}],"predecessor-version":[{"id":1866,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1707\/revisions\/1866"}],"wp:attachment":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}