{"id":53144,"date":"2025-08-12T01:19:18","date_gmt":"2025-08-11T17:19:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/53144.html"},"modified":"2025-08-12T01:19:18","modified_gmt":"2025-08-11T17:19:18","slug":"android-%e4%b9%8b-%e6%95%b0%e6%8d%ae%e8%a7%a3%e6%9e%90-json%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/53144.html","title":{"rendered":"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790"},"content":{"rendered":"<h3>\u00a0Gson<\/h3>\n<p>\u57fa\u4e8eGson\u5c01\u88c5\u7684\u5de5\u5177\u7c7b\u53ca\u4f7f\u7528\u793a\u4f8b&#xff0c;\u652f\u6301\u590d\u6742\u6570\u636e\u7ed3\u6784&#xff08;\u5d4c\u5957\u5bf9\u8c61\u3001\u6cdb\u578b\u96c6\u5408\u7b49&#xff09;&#xff0c;\u5e76\u5305\u542b\u8be6\u7ec6\u6ce8\u91ca\u548c\u6700\u4f73\u5b9e\u8df5<\/p>\n<h4>\u00a0\u200b\u200b\u4e00\u3001\u4f9d\u8d56\u914d\u7f6e\u200b\u200b<\/h4>\n<p>\u5728\u9879\u76ee\u7684\u00a0build.gradle\u00a0\u4e2d\u6dfb\u52a0\u4f9d\u8d56&#xff1a;<\/p>\n<p>dependencies {<br \/>\n    implementation &#039;com.google.code.gson:gson:2.10.1&#039;<br \/>\n}<\/p>\n<h4>\u4e8c\u3001\u57fa\u672c\u4f7f\u7528<\/h4>\n<h5>1.\u5de5\u5177\u7c7b\u5c01\u88c5<\/h5>\n<p>import com.google.gson.Gson;<br \/>\nimport com.google.gson.GsonBuilder;<br \/>\nimport com.google.gson.reflect.TypeToken;<br \/>\nimport java.lang.reflect.Type;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Map;<\/p>\n<p>\/**<br \/>\n * Gson\u5de5\u5177\u7c7b&#xff08;\u7ebf\u7a0b\u5b89\u5168&#xff09;<br \/>\n * \u529f\u80fd&#xff1a;\u5bf9\u8c61\u4e0eJSON\u4e92\u8f6c\u3001\u6cdb\u578b\u96c6\u5408\u5904\u7406\u3001\u683c\u5f0f\u5316\u8f93\u51fa<br \/>\n *\/<br \/>\npublic class GsonUtils {<br \/>\n    \/\/ \u5355\u4f8b\u6a21\u5f0f &#043; \u81ea\u5b9a\u4e49\u914d\u7f6e&#xff08;\u683c\u5f0f\u5316\/\u5e8f\u5217\u5316\u7a7a\u503c&#xff09;<br \/>\n    private static final Gson GSON &#061; new GsonBuilder()<br \/>\n        .setPrettyPrinting()    \/\/ \u683c\u5f0f\u5316JSON&#xff08;\u4fbf\u4e8e\u9605\u8bfb&#xff09;<br \/>\n        .serializeNulls()       \/\/ \u5e8f\u5217\u5316null\u503c\u5b57\u6bb5<br \/>\n        .create();<\/p>\n<p>    \/**<br \/>\n     * \u5bf9\u8c61\u8f6cJSON\u5b57\u7b26\u4e32<br \/>\n     * &#064;param object Java\u5bf9\u8c61<br \/>\n     * &#064;return JSON\u5b57\u7b26\u4e32<br \/>\n     *\/<br \/>\n    public static String toJson(Object object) {<br \/>\n        return GSON.toJson(object);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * JSON\u5b57\u7b26\u4e32\u8f6c\u5bf9\u8c61<br \/>\n     * &#064;param json  JSON\u5b57\u7b26\u4e32<br \/>\n     * &#064;param clazz \u76ee\u6807\u5bf9\u8c61\u7684Class<br \/>\n     * &#064;param &lt;T&gt;   \u76ee\u6807\u7c7b\u578b<br \/>\n     * &#064;return \u89e3\u6790\u540e\u7684\u5bf9\u8c61<br \/>\n     *\/<br \/>\n    public static &lt;T&gt; T fromJson(String json, Class&lt;T&gt; clazz) {<br \/>\n        return GSON.fromJson(json, clazz);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * JSON\u8f6cList&#xff08;\u652f\u6301\u6cdb\u578b&#xff09;<br \/>\n     * &#064;param json  JSON\u5b57\u7b26\u4e32<br \/>\n     * &#064;param clazz List\u5143\u7d20\u7684Class<br \/>\n     * &#064;param &lt;T&gt;   \u5143\u7d20\u7c7b\u578b<br \/>\n     * &#064;return \u89e3\u6790\u540e\u7684List<br \/>\n     *\/<br \/>\n    public static &lt;T&gt; List&lt;T&gt; fromJsonToList(String json, Class&lt;T&gt; clazz) {<br \/>\n        Type type &#061; TypeToken.getParameterized(List.class, clazz).getType();<br \/>\n        return GSON.fromJson(json, type);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * JSON\u8f6cMap&#xff08;\u652f\u6301\u6cdb\u578b&#xff09;<br \/>\n     * &#064;param json JSON\u5b57\u7b26\u4e32<br \/>\n     * &#064;return \u89e3\u6790\u540e\u7684Map<br \/>\n     *\/<br \/>\n    public static &lt;K, V&gt; Map&lt;K, V&gt; fromJsonToMap(String json) {<br \/>\n        Type type &#061; new TypeToken&lt;Map&lt;K, V&gt;&gt;() {}.getType();<br \/>\n        return GSON.fromJson(json, type);<br \/>\n    }<br \/>\n}<\/p>\n<h5>2.\u4f7f\u7528\u793a\u4f8b&#xff08;\u542b\u590d\u6742\u6570\u636e\u7ed3\u6784&#xff09;<\/h5>\n<p>public class User {<br \/>\n    private String name;<br \/>\n    private int age;<br \/>\n    private Address address;          \/\/ \u5d4c\u5957\u5bf9\u8c61<br \/>\n    private List&lt;String&gt; hobbies;     \/\/ List\u5b57\u6bb5<br \/>\n    private Map&lt;String, String&gt; metadata; \/\/ Map\u5b57\u6bb5<\/p>\n<p>    \/\/ \u5fc5\u987b\u6709\u65e0\u53c2\u6784\u9020\u65b9\u6cd5&#xff08;Gson\u53cd\u5e8f\u5217\u5316\u9700\u8981&#xff09;[2](&#064;ref)<br \/>\n    public User() {}<\/p>\n<p>    \/\/ Getter\/Setter\u65b9\u6cd5\u7701\u7565&#xff08;\u5b9e\u9645\u5f00\u53d1\u4e2d\u9700\u8865\u5145&#xff09;<br \/>\n}<\/p>\n<p>public class Address {<br \/>\n    private String city;<br \/>\n    private String zipCode;<\/p>\n<p>    public Address() {}<br \/>\n    \/\/ Getter\/Setter\u7701\u7565<br \/>\n}<\/p>\n<h5>3. \u5e8f\u5217\u5316&#xff08;\u5bf9\u8c61 \u2192 JSON&#xff09;<\/h5>\n<p>\/\/ \u6784\u5efa\u590d\u6742\u5bf9\u8c61<br \/>\nAddress address &#061; new Address(&#034;New York&#034;, &#034;10001&#034;);<br \/>\nList&lt;String&gt; hobbies &#061; Arrays.asList(&#034;Reading&#034;, &#034;Traveling&#034;);<br \/>\nMap&lt;String, String&gt; metadata &#061; new HashMap&lt;&gt;();<br \/>\nmetadata.put(&#034;role&#034;, &#034;admin&#034;);<br \/>\nmetadata.put(&#034;department&#034;, &#034;IT&#034;);<\/p>\n<p>User user &#061; new User(&#034;John&#034;, 25, &#034;john&#064;example.com&#034;, address, hobbies, metadata);<\/p>\n<p>\/\/ \u8f6c\u6362\u4e3aJSON&#xff08;\u683c\u5f0f\u5316\u8f93\u51fa&#xff09;<br \/>\nString json &#061; GsonUtils.toJson(user);<br \/>\nSystem.out.println(json);<\/p>\n<h5>4.\u8f93\u51fa\u7ed3\u679c\u200b\u200b<\/h5>\n<p>{<br \/>\n  &#034;name&#034;: &#034;John&#034;,<br \/>\n  &#034;age&#034;: 25,<br \/>\n  &#034;email&#034;: &#034;john&#064;example.com&#034;,<br \/>\n  &#034;address&#034;: {<br \/>\n    &#034;city&#034;: &#034;New York&#034;,<br \/>\n    &#034;zipCode&#034;: &#034;10001&#034;<br \/>\n  },<br \/>\n  &#034;hobbies&#034;: [&#034;Reading&#034;, &#034;Traveling&#034;],<br \/>\n  &#034;metadata&#034;: {<br \/>\n    &#034;role&#034;: &#034;admin&#034;,<br \/>\n    &#034;department&#034;: &#034;IT&#034;<br \/>\n  }<br \/>\n}<\/p>\n<h5>5. \u53cd\u5e8f\u5217\u5316&#xff08;JSON \u2192 \u5bf9\u8c61&#xff09;\u200b<\/h5>\n<p>\/\/ JSON\u5b57\u7b26\u4e32\u8f6c\u5bf9\u8c61<br \/>\nString json &#061; &#034;{\\\\&#034;name\\\\&#034;:\\\\&#034;Alice\\\\&#034;,\\\\&#034;age\\\\&#034;:30,\\\\&#034;address\\\\&#034;:{\\\\&#034;city\\\\&#034;:\\\\&#034;Paris\\\\&#034;}}&#034;;<br \/>\nUser user &#061; GsonUtils.fromJson(json, User.class);<br \/>\nSystem.out.println(user.getAddress().getCity()); \/\/ \u8f93\u51fa&#xff1a;Paris<\/p>\n<p>\/\/ JSON\u8f6cList&#xff08;\u6cdb\u578b\u652f\u6301&#xff09;<br \/>\nString listJson &#061; &#034;[{\\\\&#034;name\\\\&#034;:\\\\&#034;Bob\\\\&#034;}, {\\\\&#034;name\\\\&#034;:\\\\&#034;Charlie\\\\&#034;}]&#034;;<br \/>\nList&lt;User&gt; userList &#061; GsonUtils.fromJsonToList(listJson, User.class);<br \/>\nSystem.out.println(userList.size()); \/\/ \u8f93\u51fa&#xff1a;2<\/p>\n<p>\/\/ JSON\u8f6cMap<br \/>\nString mapJson &#061; &#034;{\\\\&#034;key1\\\\&#034;:\\\\&#034;value1\\\\&#034;, \\\\&#034;key2\\\\&#034;:\\\\&#034;value2\\\\&#034;}&#034;;<br \/>\nMap&lt;String, String&gt; map &#061; GsonUtils.fromJsonToMap(mapJson);<br \/>\nSystem.out.println(map.get(&#034;key2&#034;)); \/\/ \u8f93\u51fa&#xff1a;value2<\/p>\n<h5>6.\u65e5\u5e38\u4f7f\u7528<\/h5>\n<p>1.\u5b9a\u5236\u5de5\u5177\u7c7b<\/p>\n<p> public final class JSONUtils {<br \/>\n \/\/\u89e3\u6790jsonobject,\u4ee5{}\u5f00\u5934\u548c\u7ed3\u5c3e<br \/>\npublic static &lt;T&gt; T parseJSON(String jsonStr, Class&lt;T&gt; t) {<br \/>\nGson gson &#061; new Gson();<br \/>\nT bean &#061; gson.fromJson(jsonStr, t);<br \/>\nreturn bean;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \/\/jsonarry,\u4ee5[]\u5f00\u5934\u548c\u7ed3\u5c3e<br \/>\n * &#064;param response<br \/>\n * &#064;param type<br \/>\n *           Type type &#061; new TypeToken&lt; ArrayList&lt;\u5b9e\u4f53\u7c7b\u540d&gt;&gt;() {  }.getType();<br \/>\n\/\/ *<br \/>\n * &#064;return<br \/>\n *\/<br \/>\npublic static &lt;T&gt; T parseJSONArray(String response, Type type) {<br \/>\nGson gson &#061; new Gson();<br \/>\nT data &#061; gson.fromJson(response, type);<br \/>\nreturn data;<br \/>\n}<br \/>\n\/\/\u628amap\u8f6c\u4e3aString,\u5f88\u5c11\u7528<br \/>\npublic static &lt;T&gt; String mapToJson(Map&lt;String, T&gt; map) {<br \/>\nGson gson &#061; new Gson();<br \/>\nString jsonStr &#061; gson.toJson(map);<br \/>\nreturn jsonStr;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>2.Java Bean<\/p>\n<p>public class Man {<\/p>\n<p>    \/**<br \/>\n     * name : \u5f20\u4e09<br \/>\n     * age : 18<br \/>\n     *\/<\/p>\n<p>    private String name;<br \/>\n    private int age;<\/p>\n<p>    public String getName() {<br \/>\n        return name;<br \/>\n    }<\/p>\n<p>    public void setName(String name) {<br \/>\n        this.name &#061; name;<br \/>\n    }<\/p>\n<p>    public int getAge() {<br \/>\n        return age;<br \/>\n    }<\/p>\n<p>    public void setAge(int age) {<br \/>\n        this.age &#061; age;<br \/>\n    }<br \/>\n}<\/p>\n<p>2.\u51c6\u5907\u5b57\u7b26\u4e32\u6570\u636e<\/p>\n<p>{&#034;error_code&#034;:10005,&#034;reason&#034;:&#034;\u5e94\u7528\u672a\u5ba1\u6838\u8d85\u65f6&#xff0c;\u8bf7\u63d0\u4ea4\u8ba4\u8bc1&#034;}<\/p>\n<p>3.\u89e3\u6790\u5b57\u7b26\u4e32\u6570\u636e<\/p>\n<p>  Man man &#061; JSONUtils.parseJSON(ST, Man.class);<br \/>\n  \/\/\u83b7\u5f97\u9519\u8bef\u7801<br \/>\n  int error_code &#061; man.getError_code();<br \/>\n  \/\/\u9519\u8bef\u539f\u56e0<br \/>\n   String reason &#061; man.getReason();<\/p>\n<p>4.\u51c6\u5907\u6570\u7ec4\u6570\u636e<br \/>\n[<br \/>\n    {<br \/>\n        &#034;name&#034;: &#034;\u5f20\u4e09&#034;,<br \/>\n        &#034;age&#034;: 18<br \/>\n    },<br \/>\n    {<br \/>\n        &#034;name&#034;: &#034;\u674e\u56db&#034;,<br \/>\n        &#034;age&#034;: 19<br \/>\n    }<br \/>\n]<\/p>\n<p>5.\u89e3\u6790\u6570\u7ec4\u6570\u636e<\/p>\n<p> Type type &#061; new TypeToken&lt;ArrayList&lt;Man&gt;&gt;() {  }.getType();<br \/>\n \/\/\u83b7\u5f97\u4e86\u4e00\u4e2aMan(\u7c7b\u540d)\u7684\u96c6\u5408<br \/>\n                ArrayList&lt;Man&gt; first  &#061; JSONUtils.parseJSONArray(st, type);<br \/>\n                 \/\/\u83b7\u5f97\u7b2c\u4e00\u4e2a\u4eba\u7684\u5e74\u9f84<br \/>\n                int age &#061; first.get(0).getAge();<br \/>\n                Log.e(&#034;888&#034;, &#034;&#034;&#043;age);<br \/>\n                \u6253\u5370\u4e0b\u662f18\u5c81\u54e6&#xff01;<\/p>\n<p>\u6ce8\u610f\u5bfc\u5305\u5bfc\u8fd9\u4e2a&#xff0c;\u4e0d\u7136\u4f1a\u51fa\u9519<br \/>\nimport java.lang.reflect.Type;<br \/>\nimport com.google.gson.reflect.TypeToken;<\/p>\n<h3>Jackson<\/h3>\n<h2><\/h2>\n<h4>\u00a0\u200b\u200b\u4e00\u3001\u4f9d\u8d56\u914d\u7f6e\u200b\u200b<\/h4>\n<p>\u5728\u9879\u76ee\u7684\u00a0build.gradle\u00a0\u4e2d\u6dfb\u52a0\u4f9d\u8d56&#xff1a;<\/p>\n<p>\/\/jackson-databind&#xff1a;\u6838\u5fc3\u5e93&#xff08;\u81ea\u52a8\u5305\u542bjackson-core\u548cjackson-annotations&#xff09;<br \/>\n\/\/jackson-datatype-jsr310&#xff1a;\u652f\u6301LocalDateTime\u7b49Java 8\u65e5\u671f\u7c7b\u578b<\/p>\n<p>dependencies {<br \/>\n    implementation &#039;com.fasterxml.jackson.core:jackson-databind:2.15.2&#039;<br \/>\n    implementation &#039;com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2&#039;<br \/>\n}<\/p>\n<h4>\u4e8c\u3001\u57fa\u672c\u4f7f\u7528<\/h4>\n<h5>1.\u5de5\u5177\u7c7b\u5c01\u88c5<\/h5>\n<p>import com.fasterxml.jackson.core.JsonProcessingException;<br \/>\nimport com.fasterxml.jackson.core.type.TypeReference;<br \/>\nimport com.fasterxml.jackson.databind.*;<br \/>\nimport com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;<br \/>\nimport java.text.SimpleDateFormat;<br \/>\nimport java.util.List;<br \/>\nimport java.util.Map;<\/p>\n<p>public class JacksonUtil {<br \/>\n    private static final ObjectMapper mapper &#061; new ObjectMapper();<\/p>\n<p>    static {<br \/>\n        \/\/ \u57fa\u7840\u914d\u7f6e<br \/>\n        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); \/\/ \u5ffd\u7565\u672a\u77e5\u5b57\u6bb5[6](&#064;ref)<br \/>\n        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); \/\/ \u5ffd\u7565null\u5b57\u6bb5[7](&#064;ref)<br \/>\n        mapper.setDateFormat(new SimpleDateFormat(&#034;yyyy-MM-dd HH:mm:ss&#034;)); \/\/ \u7edf\u4e00\u65e5\u671f\u683c\u5f0f<\/p>\n<p>        \/\/ \u6ce8\u518cJava 8\u65e5\u671f\u6a21\u5757<br \/>\n        mapper.registerModule(new JavaTimeModule());<br \/>\n        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); \/\/ \u7981\u7528\u65f6\u95f4\u6233\u683c\u5f0f[7](&#064;ref)<br \/>\n    }<\/p>\n<p>    \/\/ \u5bf9\u8c61 \u2192 JSON\u5b57\u7b26\u4e32<br \/>\n    public static String toJson(Object obj) {<br \/>\n        try {<br \/>\n            return mapper.writeValueAsString(obj);<br \/>\n        } catch (JsonProcessingException e) {<br \/>\n            throw new RuntimeException(&#034;JSON\u5e8f\u5217\u5316\u5931\u8d25&#034;, e);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ JSON\u5b57\u7b26\u4e32 \u2192 \u5bf9\u8c61<br \/>\n    public static &lt;T&gt; T toObject(String json, Class&lt;T&gt; clazz) {<br \/>\n        try {<br \/>\n            return mapper.readValue(json, clazz);<br \/>\n        } catch (JsonProcessingException e) {<br \/>\n            throw new RuntimeException(&#034;JSON\u53cd\u5e8f\u5217\u5316\u5931\u8d25&#034;, e);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ JSON\u5b57\u7b26\u4e32 \u2192 \u590d\u6742\u96c6\u5408&#xff08;\u5982List&lt;User&gt;&#xff09;<br \/>\n    public static &lt;T&gt; List&lt;T&gt; toList(String json, Class&lt;T&gt; elementClass) {<br \/>\n        try {<br \/>\n            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, elementClass));<br \/>\n        } catch (JsonProcessingException e) {<br \/>\n            throw new RuntimeException(&#034;JSON\u8f6cList\u5931\u8d25&#034;, e);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ JSON\u5b57\u7b26\u4e32 \u2192 \u5d4c\u5957\u7ed3\u6784&#xff08;\u5982Map&lt;String, List&lt;Order&gt;&gt;&#xff09;<br \/>\n    public static &lt;T&gt; T toComplexObject(String json, TypeReference&lt;T&gt; typeRef) {<br \/>\n        try {<br \/>\n            return mapper.readValue(json, typeRef);<br \/>\n        } catch (JsonProcessingException e) {<br \/>\n            throw new RuntimeException(&#034;\u590d\u6742\u7ed3\u6784\u8f6c\u6362\u5931\u8d25&#034;, e);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h5>2.\u57fa\u7840\u5bf9\u8c61\u8f6c\u6362\u200b<\/h5>\n<p>\/\/ \u5b9a\u4e49\u5b9e\u4f53\u7c7b<br \/>\npublic class User {<br \/>\n    private String name;<br \/>\n    private Integer age;<br \/>\n    private LocalDateTime createTime;<br \/>\n    \/\/ Getter\/Setter\u7701\u7565<br \/>\n}<\/p>\n<p>\/\/ \u5e8f\u5217\u5316\u5bf9\u8c61 \u2192 JSON<br \/>\nUser user &#061; new User(&#034;\u5f20\u4e09&#034;, 25, LocalDateTime.now());<br \/>\nString json &#061; JacksonUtil.toJson(user);<br \/>\n\/\/ \u8f93\u51fa: {&#034;name&#034;:&#034;\u5f20\u4e09&#034;,&#034;age&#034;:25,&#034;createTime&#034;:&#034;2025-08-03 14:30:00&#034;} [3](&#064;ref)<\/p>\n<p>\/\/ \u53cd\u5e8f\u5217\u5316 JSON \u2192 \u5bf9\u8c61<br \/>\nUser parsedUser &#061; JacksonUtil.toObject(json, User.class);<br \/>\nSystem.out.println(parsedUser.getName()); \/\/ \u8f93\u51fa: \u5f20\u4e09<\/p>\n<h5>3.\u96c6\u5408\u5904\u7406<\/h5>\n<p>\/\/ List\u5e8f\u5217\u5316<br \/>\nList&lt;User&gt; userList &#061; Arrays.asList(<br \/>\n    new User(&#034;\u674e\u56db&#034;, 30, null),<br \/>\n    new User(&#034;\u738b\u4e94&#034;, 28, LocalDateTime.now())<br \/>\n);<br \/>\nString listJson &#061; JacksonUtil.toJson(userList);<br \/>\n\/\/ \u8f93\u51fa: [{&#034;name&#034;:&#034;\u674e\u56db&#034;,&#034;age&#034;:30},{&#034;name&#034;:&#034;\u738b\u4e94&#034;,&#034;age&#034;:28,&#034;createTime&#034;:&#034;2025-08-03 14:35:00&#034;}]<\/p>\n<p>\/\/ List\u53cd\u5e8f\u5217\u5316<br \/>\nList&lt;User&gt; parsedList &#061; JacksonUtil.toList(listJson, User.class);<\/p>\n<h5>4.\u00a0\u5de5\u5177\u7c7b\u65b9\u6cd5\u603b\u7ed3<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"305\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811171917-689a2615719d6.png\" width=\"1316\" \/><\/p>\n<h3>fastjson<\/h3>\n<h2><\/h2>\n<h4>\u00a0\u200b\u200b\u4e00\u3001\u4f9d\u8d56\u914d\u7f6e\u200b\u200b<\/h4>\n<p>\u5728\u9879\u76ee\u7684\u00a0build.gradle\u00a0\u4e2d\u6dfb\u52a0\u4f9d\u8d56&#xff1a;<\/p>\n<p>\/\/jackson-databind&#xff1a;\u6838\u5fc3\u5e93&#xff08;\u81ea\u52a8\u5305\u542bjackson-core\u548cjackson-annotations&#xff09;<br \/>\n\/\/jackson-datatype-jsr310&#xff1a;\u652f\u6301LocalDateTime\u7b49Java 8\u65e5\u671f\u7c7b\u578b<\/p>\n<p>dependencies {<br \/>\n    implementation &#039;com.alibaba:fastjson:2.0.39&#039;<br \/>\n}<\/p>\n<h4>\u4e8c\u3001\u57fa\u672c\u4f7f\u7528<\/h4>\n<h5>1.\u5de5\u5177\u7c7b\u5c01\u88c5<\/h5>\n<p>import com.alibaba.fastjson2.*;<br \/>\nimport com.alibaba.fastjson2.filter.Filter;<br \/>\nimport java.util.List;<\/p>\n<p>public class FastJsonUtil {<br \/>\n    private static final ParserConfig SAFE_PARSER_CONFIG;<\/p>\n<p>    static {<br \/>\n        \/\/ \u5b89\u5168\u914d\u7f6e&#xff1a;\u542f\u7528\u5b89\u5168\u6a21\u5f0f&#xff0c;\u7981\u6b62\u53cd\u5e8f\u5217\u5316\u4efb\u610f\u7c7b [7](&#064;ref)<br \/>\n        SAFE_PARSER_CONFIG &#061; new ParserConfig();<br \/>\n        SAFE_PARSER_CONFIG.setSafeMode(true);<\/p>\n<p>        \/\/ \u53ef\u9009&#xff1a;\u8bbe\u7f6e\u767d\u540d\u5355&#xff08;\u4ec5\u5141\u8bb8\u6307\u5b9a\u5305\u4e0b\u7684\u7c7b&#xff09;[7](&#064;ref)<br \/>\n        SAFE_PARSER_CONFIG.addAccept(&#034;com.yourpackage.model.&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u5bf9\u8c61 \u2192 JSON \u5b57\u7b26\u4e32&#xff08;\u9ed8\u8ba4\u914d\u7f6e&#xff09;<br \/>\n    public static String toJson(Object obj) {<br \/>\n        return JSON.toJSONString(obj);<br \/>\n    }<\/p>\n<p>    \/\/ \u5bf9\u8c61 \u2192 JSON \u5b57\u7b26\u4e32&#xff08;\u81ea\u5b9a\u4e49\u914d\u7f6e&#xff09;<br \/>\n    public static String toJson(Object obj, JSONWriter.Feature&#8230; features) {<br \/>\n        return JSON.toJSONString(obj, features);<br \/>\n    }<\/p>\n<p>    \/\/ JSON \u5b57\u7b26\u4e32 \u2192 \u5bf9\u8c61&#xff08;\u5b89\u5168\u6a21\u5f0f&#xff09;<br \/>\n    public static &lt;T&gt; T toBean(String json, Class&lt;T&gt; clazz) {<br \/>\n        return JSON.parseObject(json, clazz, SAFE_PARSER_CONFIG);<br \/>\n    }<\/p>\n<p>    \/\/ JSON \u5b57\u7b26\u4e32 \u2192 \u6cdb\u578b\u96c6\u5408&#xff08;\u5982 List&lt;User&gt;&#xff09;<br \/>\n    public static &lt;T&gt; List&lt;T&gt; toList(String json, Class&lt;T&gt; clazz) {<br \/>\n        return JSON.parseArray(json, clazz, SAFE_PARSER_CONFIG);<br \/>\n    }<\/p>\n<p>    \/\/ JSON \u5b57\u7b26\u4e32 \u2192 \u590d\u6742\u5d4c\u5957\u7ed3\u6784&#xff08;\u5982 Map&lt;String, List&gt;&#xff09;<br \/>\n    public static &lt;T&gt; T toComplexType(String json, TypeReference&lt;T&gt; typeRef) {<br \/>\n        return JSON.parseObject(json, typeRef, SAFE_PARSER_CONFIG);<br \/>\n    }<br \/>\n}<\/p>\n<h5>2. \u200b\u200b\u57fa\u7840\u5bf9\u8c61\u8f6c\u6362<\/h5>\n<p>\/\/ \u5b9a\u4e49\u5b9e\u4f53\u7c7b<br \/>\npublic class User {<br \/>\n    &#064;JSONField(name &#061; &#034;user_name&#034;) \/\/ \u81ea\u5b9a\u4e49\u5b57\u6bb5\u540d [6,7](&#064;ref)<br \/>\n    private String name;<\/p>\n<p>    &#064;JSONField(serialize &#061; false) \/\/ \u5ffd\u7565\u5e8f\u5217\u5316 [6](&#064;ref)<br \/>\n    private String password;<\/p>\n<p>    &#064;JSONField(format &#061; &#034;yyyy-MM-dd&#034;) \/\/ \u65e5\u671f\u683c\u5f0f\u5316 [7](&#064;ref)<br \/>\n    private Date birthDate;<\/p>\n<p>    \/\/ Getter\/Setter \u7701\u7565<br \/>\n}<\/p>\n<p>\/\/ \u5e8f\u5217\u5316\u5bf9\u8c61 \u2192 JSON<br \/>\nUser user &#061; new User(&#034;\u5f20\u4e09&#034;, &#034;123456&#034;, new Date());<br \/>\nString json &#061; FastJsonUtil.toJson(user);<br \/>\n\/\/ \u8f93\u51fa&#xff1a;{&#034;user_name&#034;:&#034;\u5f20\u4e09&#034;,&#034;birthDate&#034;:&#034;2025-08-03&#034;}<\/p>\n<p>\/\/ \u53cd\u5e8f\u5217\u5316 JSON \u2192 \u5bf9\u8c61<br \/>\nUser parsedUser &#061; FastJsonUtil.toBean(json, User.class);<br \/>\nSystem.out.println(parsedUser.getName()); \/\/ \u8f93\u51fa&#xff1a;\u5f20\u4e09<\/p>\n<h5>3. \u200b\u200b\u96c6\u5408\u5904\u7406\u200b<\/h5>\n<p>\/\/ List \u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316<br \/>\nList&lt;User&gt; userList &#061; Arrays.asList(<br \/>\n    new User(&#034;\u674e\u56db&#034;, null, null),<br \/>\n    new User(&#034;\u738b\u4e94&#034;, &#034;abc&#034;, new Date())<br \/>\n);<br \/>\nString listJson &#061; FastJsonUtil.toJson(userList, JSONWriter.Feature.PrettyFormat);<\/p>\n<p>List&lt;User&gt; parsedList &#061; FastJsonUtil.toList(listJson, User.class);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb179\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u200b\u4e00\u3001\u4f9d\u8d56\u914d\u7f6e\u200b\u3002<\/p>\n","protected":false},"author":2,"featured_media":53143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[84,93],"topic":[],"class_list":["post-53144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-android","tag-json"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/53144.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb179\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u200b\u4e00\u3001\u4f9d\u8d56\u914d\u7f6e\u200b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/53144.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T17:19:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811171917-689a2615719d6.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53144.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/53144.html\",\"name\":\"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T17:19:18+00:00\",\"dateModified\":\"2025-08-11T17:19:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/53144.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/53144.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53144.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/53144.html","og_locale":"zh_CN","og_type":"article","og_title":"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb179\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u200b\u4e00\u3001\u4f9d\u8d56\u914d\u7f6e\u200b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/53144.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T17:19:18+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811171917-689a2615719d6.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/53144.html","url":"https:\/\/www.wsisp.com\/helps\/53144.html","name":"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T17:19:18+00:00","dateModified":"2025-08-11T17:19:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/53144.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/53144.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/53144.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Android \u4e4b \u6570\u636e\u89e3\u6790 - Json\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=53144"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53144\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/53143"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=53144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=53144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=53144"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=53144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}