{"id":599,"date":"2025-02-01T14:20:56","date_gmt":"2025-02-01T06:20:56","guid":{"rendered":"https:\/\/www.huangkeqin.cn\/?p=599"},"modified":"2025-02-01T14:23:53","modified_gmt":"2025-02-01T06:23:53","slug":"%e4%bb%80%e4%b9%88%e6%98%af%e5%ba%8f%e5%88%97%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.huangkeqin.cn\/index.php\/2025\/02\/01\/%e4%bb%80%e4%b9%88%e6%98%af%e5%ba%8f%e5%88%97%e5%8c%96\/","title":{"rendered":"\u4ec0\u4e48\u662f\u5e8f\u5217\u5316"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>\u4ec0\u4e48\u662f\u5e8f\u5217\u5316\uff08Serialization\uff09\uff1f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e8f\u5217\u5316\uff08Serialization\uff09<\/strong> \u662f\u6307<strong>\u628a\u5bf9\u8c61\u8f6c\u6362\u6210\u53ef\u4ee5\u5b58\u50a8\u6216\u4f20\u8f93\u7684\u683c\u5f0f<\/strong>\uff0c\u8fd9\u6837\u6570\u636e\u53ef\u4ee5\u5728\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u4f20\u8f93\uff0c\u6216\u8005\u5b58\u50a8\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f13\u5b58\u7b49\u5730\u65b9\u3002<br><strong>\u53cd\u5e8f\u5217\u5316\uff08Deserialization\uff09<\/strong> \u5219\u662f\u5e8f\u5217\u5316\u7684\u9006\u8fc7\u7a0b\uff0c\u5373<strong>\u628a\u5b58\u50a8\u6216\u4f20\u8f93\u7684\u5b57\u8282\u6570\u636e\u8f6c\u6362\u56de\u5bf9\u8c61<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u5e8f\u5217\u5316\uff1f<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Java \u91cc\uff0c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5bf9\u8c61\uff08\u5982 <code>User<\/code> \u7c7b\u7684\u5b9e\u4f8b\uff09\u662f\u5b58\u50a8\u5728 <strong>\u5185\u5b58\uff08JVM \u5806\uff09<\/strong> \u91cc\u7684\uff0c\u800c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5c06\u5bf9\u8c61\u5b58\u5165\u6587\u4ef6\u3001\u6570\u636e\u5e93<\/strong>\uff08\u5982 MySQL\u3001Redis\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u5bf9\u8c61<\/strong>\uff08\u5982 Web API\u3001RPC \u8c03\u7528\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7f13\u5b58\u5bf9\u8c61<\/strong>\uff08\u5982 Redis\u3001Ehcache\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8de8\u8fdb\u7a0b\u901a\u4fe1<\/strong>\uff08\u5982 Java RMI\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46 Java \u7684\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u662f\u590d\u6742\u7684 <strong>\u6570\u636e\u7ed3\u6784<\/strong>\uff0c\u4e0d\u80fd\u76f4\u63a5\u5b58\u5230\u6587\u4ef6\u6216\u7f51\u7edc\u4e2d\u3002<br>\u6240\u4ee5\uff0c\u6211\u4eec<strong>\u9700\u8981\u628a\u5bf9\u8c61\u8f6c\u6362\u6210\u53ef\u4ee5\u5b58\u50a8\u6216\u4f20\u8f93\u7684\u683c\u5f0f<\/strong>\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSON<\/strong><\/li>\n\n\n\n<li><strong>XML<\/strong><\/li>\n\n\n\n<li><strong>\u4e8c\u8fdb\u5236\u6d41<\/strong><\/li>\n\n\n\n<li><strong>Protocol Buffers\uff08Protobuf\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a<strong>\u8f6c\u6362\u7684\u8fc7\u7a0b<\/strong>\u5c31\u53eb <strong>\u5e8f\u5217\u5316<\/strong>\uff0c\u53cd\u8fc7\u6765<strong>\u6062\u590d\u5bf9\u8c61<\/strong>\u7684\u8fc7\u7a0b\u5c31\u662f <strong>\u53cd\u5e8f\u5217\u5316<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Java \u5e8f\u5217\u5316\u65b9\u5f0f<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Java \u81ea\u5e26\u5e8f\u5217\u5316\uff08JDK \u539f\u751f\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u63d0\u4f9b\u4e86 <code>Serializable<\/code> \u63a5\u53e3\uff0c\u53ef\u4ee5\u8ba9\u5bf9\u8c61\u652f\u6301 <strong>\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316<\/strong>\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\n\/\/ \u8ba9\u7c7b\u5b9e\u73b0 Serializable \u63a5\u53e3\nclass User implements Serializable {\n    private static final long serialVersionUID = 1L; \/\/ \u7528\u4e8e\u7248\u672c\u63a7\u5236\n    private String name;\n    private int age;\n\n    public User(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    @Override\n    public String toString() {\n        return \"User{name='\" + name + \"', age=\" + age + \"}\";\n    }\n}\n\npublic class TestSerialization {\n    public static void main(String&#91;] args) throws IOException, ClassNotFoundException {\n        \/\/ \u5e8f\u5217\u5316\uff1a\u5bf9\u8c61\u5199\u5165\u6587\u4ef6\n        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(\"user.ser\"));\n        User user = new User(\"\u5f20\u4e09\", 25);\n        oos.writeObject(user);\n        oos.close();\n\n        \/\/ \u53cd\u5e8f\u5217\u5316\uff1a\u4ece\u6587\u4ef6\u8bfb\u53d6\u5bf9\u8c61\n        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"user.ser\"));\n        User deserializedUser = (User) ois.readObject();\n        ois.close();\n\n        System.out.println(\"\u53cd\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61\uff1a\" + deserializedUser);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u53cd\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61\uff1aUser{name='\u5f20\u4e09', age=25}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc <code>Serializable<\/code> <strong>\u81ea\u52a8\u5c06\u5bf9\u8c61\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236<\/strong>\uff0c\u5e76\u5b58\u5165 <code>user.ser<\/code> \u6587\u4ef6\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. JSON \u5e8f\u5217\u5316\uff08Spring Boot \u5e38\u7528\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\u662f <strong>JSON<\/strong>\uff0c\u56e0\u4e3a JSON \u662f<strong>\u53ef\u8bfb\u6027\u5f3a\u3001\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\u7684\u683c\u5f0f\u3002<br>Spring Boot \u9ed8\u8ba4\u4f7f\u7528 <strong>Jackson<\/strong> \u5e93\u6765\u8fdb\u884c JSON \u5e8f\u5217\u5316\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.fasterxml.jackson.databind.ObjectMapper;\n\nclass User {\n    private String name;\n    private int age;\n\n    public User(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    public String getName() { return name; }\n    public int getAge() { return age; }\n}\n\npublic class JsonSerialization {\n    public static void main(String&#91;] args) throws Exception {\n        ObjectMapper objectMapper = new ObjectMapper();\n        User user = new User(\"\u5f20\u4e09\", 25);\n\n        \/\/ \u5e8f\u5217\u5316\uff1a\u5bf9\u8c61 -&gt; JSON\n        String jsonString = objectMapper.writeValueAsString(user);\n        System.out.println(\"JSON \u5e8f\u5217\u5316\uff1a\" + jsonString);\n\n        \/\/ \u53cd\u5e8f\u5217\u5316\uff1aJSON -&gt; \u5bf9\u8c61\n        User deserializedUser = objectMapper.readValue(jsonString, User.class);\n        System.out.println(\"\u53cd\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61\uff1a\" + deserializedUser.getName());\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JSON \u5e8f\u5217\u5316\uff1a{\"name\":\"\u5f20\u4e09\",\"age\":25}\n\u53cd\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61\uff1a\u5f20\u4e09\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc <code>ObjectMapper<\/code> <strong>\u81ea\u52a8\u628a\u5bf9\u8c61\u8f6c\u6362\u6210 JSON<\/strong>\uff0c\u5e76\u53ef\u4ee5<strong>\u53cd\u5e8f\u5217\u5316\u56de Java \u5bf9\u8c61<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. XML \u5e8f\u5217\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8981\u628a\u5bf9\u8c61\u8f6c\u6362\u6210 <strong>XML<\/strong>\uff08\u7528\u4e8e Web Service\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\uff09\uff0c\u53ef\u4ee5\u7528 <code>javax.xml.bind.JAXB<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import javax.xml.bind.annotation.XmlElement;\nimport javax.xml.bind.annotation.XmlRootElement;\nimport javax.xml.bind.JAXBContext;\nimport javax.xml.bind.JAXBException;\nimport javax.xml.bind.Marshaller;\nimport java.io.StringWriter;\n\n@XmlRootElement\nclass User {\n    private String name;\n    private int age;\n\n    @XmlElement\n    public String getName() { return name; }\n\n    @XmlElement\n    public int getAge() { return age; }\n\n    public User() {} \/\/ JAXB \u9700\u8981\u65e0\u53c2\u6784\u9020\u5668\n    public User(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n}\n\npublic class XmlSerialization {\n    public static void main(String&#91;] args) throws JAXBException {\n        JAXBContext context = JAXBContext.newInstance(User.class);\n        Marshaller marshaller = context.createMarshaller();\n        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);\n\n        User user = new User(\"\u5f20\u4e09\", 25);\n        StringWriter writer = new StringWriter();\n        marshaller.marshal(user, writer);\n\n        System.out.println(writer.toString());\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?&gt;\n&lt;user&gt;\n    &lt;name&gt;\u5f20\u4e09&lt;\/name&gt;\n    &lt;age&gt;25&lt;\/age&gt;\n&lt;\/user&gt;\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc <code>JAXB<\/code> <strong>\u628a\u5bf9\u8c61\u8f6c\u6362\u6210 XML \u683c\u5f0f<\/strong>\uff0c\u5e38\u7528\u4e8e Web Service\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Protobuf \u5e8f\u5217\u5316\uff08\u9ad8\u6027\u80fd\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Google \u63d0\u4f9b\u7684 <strong>Protocol Buffers\uff08Protobuf\uff09<\/strong> \u662f\u4e00\u79cd\u9ad8\u6548\u7684 <strong>\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u683c\u5f0f<\/strong>\uff0c\u9002\u7528\u4e8e\u9ad8\u6027\u80fd RPC \u901a\u4fe1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6bd4 JSON \u5c0f 10 \u500d<\/strong><\/li>\n\n\n\n<li><strong>\u901f\u5ea6\u6bd4 JSON \u5feb 5-10 \u500d<\/strong><\/li>\n\n\n\n<li><strong>\u9002\u7528\u4e8e\u5fae\u670d\u52a1\u3001\u7f51\u7edc\u901a\u4fe1<\/strong><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49 <code>.proto<\/code> \u6587\u4ef6<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>syntax = \"proto3\";\nmessage User {\n    string name = 1;\n    int32 age = 2;\n}\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u7528 Protobuf \u751f\u6210 Java \u7c7b<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>protoc --java_out=. User.proto\n<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u5e8f\u5217\u5316 \/ \u53cd\u5e8f\u5217\u5316<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>User user = User.newBuilder().setName(\"\u5f20\u4e09\").setAge(25).build();\nbyte&#91;] data = user.toByteArray(); \/\/ \u5e8f\u5217\u5316\nUser newUser = User.parseFrom(data); \/\/ \u53cd\u5e8f\u5217\u5316\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u5e8f\u5217\u5316\u65b9\u5f0f<\/strong><\/th><th><strong>\u4f18\u70b9<\/strong><\/th><th><strong>\u7f3a\u70b9<\/strong><\/th><th><strong>\u5e94\u7528\u573a\u666f<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Java <code>Serializable<\/code><\/strong><\/td><td>\u81ea\u5e26\u3001\u7b80\u5355<\/td><td>\u4f53\u79ef\u5927\u3001\u4e0d\u8de8\u8bed\u8a00<\/td><td>\u672c\u5730\u5b58\u50a8\u3001\u7f13\u5b58<\/td><\/tr><tr><td><strong>JSON\uff08Jackson\uff09<\/strong><\/td><td>\u4eba\u7c7b\u53ef\u8bfb\u3001\u8de8\u8bed\u8a00<\/td><td>\u4f53\u79ef\u5927\uff0c\u901f\u5ea6\u6162<\/td><td>API \u4f20\u8f93\u3001\u65e5\u5fd7\u5b58\u50a8<\/td><\/tr><tr><td><strong>XML\uff08JAXB\uff09<\/strong><\/td><td>\u7ed3\u6784\u6e05\u6670\u3001\u6807\u51c6\u5316<\/td><td>\u4f53\u79ef\u5927<\/td><td>Web Service\u3001\u914d\u7f6e\u6587\u4ef6<\/td><\/tr><tr><td><strong>Protobuf<\/strong><\/td><td>\u4f53\u79ef\u5c0f\u3001\u901f\u5ea6\u5feb<\/td><td>\u9700\u8981 <code>.proto<\/code> \u5b9a\u4e49<\/td><td>\u9ad8\u6027\u80fd RPC\u3001\u5fae\u670d\u52a1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u63a8\u8350\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web \u5f00\u53d1\u7528 <strong>JSON\uff08Jackson\uff09<\/strong>\u3002<\/li>\n\n\n\n<li>\u8f7b\u91cf\u7ea7\u7f51\u7edc\u901a\u4fe1\u7528 <strong>Protobuf<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u5728 Spring Boot \u91cc\u7528\u7684 <code>@JsonSerialize<\/code> \u5c31\u662f <strong>\u57fa\u4e8e JSON \u7684\u5e8f\u5217\u5316<\/strong>\uff0c\u8131\u654f\u5e8f\u5217\u5316\u672c\u8d28\u4e0a\u5c31\u662f <strong>\u5728\u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d\u4fee\u6539\u6570\u636e<\/strong>\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u91cc\u7684\u5bf9\u8c61\u672c\u8d28\u4e0a\u786e\u5b9e\u662f <strong>\u4e8c\u8fdb\u5236\u6570\u636e<\/strong>\uff0c\u4f46\u5b83\u4eec<strong>\u5728 JVM \u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u683c\u5f0f<\/strong>\u548c<strong>\u6211\u4eec\u9700\u8981\u5b58\u50a8\u6216\u4f20\u8f93\u7684\u683c\u5f0f<\/strong>\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u4ecd\u7136\u9700\u8981<strong>\u5e8f\u5217\u5316<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e3a\u4ec0\u4e48 Java \u5bf9\u8c61\u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u8fd8\u8981\u5e8f\u5217\u5316\uff1f<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. JVM \u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u683c\u5f0f\u548c\u5b58\u50a8\/\u4f20\u8f93\u7684\u683c\u5f0f\u4e0d\u540c<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Java \u91cc\uff0c<strong>\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f<\/strong>\u662f <strong>JVM \u4e13\u7528\u7684\u683c\u5f0f<\/strong>\uff0c\u5305\u542b\uff1a<ul><li>\u5bf9\u8c61\u5934\uff08Object Header\uff09<\/li><li>\u5bf9\u8c61\u7684\u5b57\u6bb5\uff08Heap Data\uff09<\/li><li>\u5bf9\u8c61\u65b9\u6cd5\u8868\uff08Method Table\uff09<\/li><\/ul>\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u662f <strong>Java \u8fd0\u884c\u65f6\u4e13\u6709<\/strong>\uff0c\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u5230\u786c\u76d8\u6216\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u3002<\/li>\n\n\n\n<li>\u4f46\u662f\uff0c\u5f53\u6211\u4eec\u8981 <strong>\u5b58\u50a8\u5230\u6587\u4ef6\/\u6570\u636e\u5e93<\/strong> \u6216 <strong>\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93<\/strong> \u65f6\uff0c<strong>\u5fc5\u987b\u8f6c\u6362\u6210\u901a\u7528\u683c\u5f0f<\/strong>\uff0c\u6bd4\u5982\uff1a\n<ul class=\"wp-block-list\">\n<li><strong>JSON<\/strong>\uff08\u53ef\u8bfb\u6027\u5f3a\uff09<\/li>\n\n\n\n<li><strong>XML<\/strong>\uff08\u6807\u51c6\u5316\uff09<\/li>\n\n\n\n<li><strong>Protobuf<\/strong>\uff08\u9ad8\u6548\u4f20\u8f93\uff09<\/li>\n\n\n\n<li><strong>\u4e8c\u8fdb\u5236\u6d41<\/strong>\uff08Java <code>Serializable<\/code>\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c<strong>\u5e8f\u5217\u5316\u7684\u76ee\u7684<\/strong>\u662f<strong>\u5c06 JVM \u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u8f6c\u6362\u6210\u53ef\u4ee5\u957f\u671f\u5b58\u50a8\u6216\u8de8\u7cfb\u7edf\u4f20\u8f93\u7684\u683c\u5f0f<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. Java \u7684\u5185\u5b58\u5bf9\u8c61\u5730\u5740\u548c\u7ed3\u6784\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u5b58\u50a8\u6216\u4f20\u8f93<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Java \u91cc\uff0c\u6240\u6709\u5bf9\u8c61\u90fd\u5b58\u50a8\u5728 <strong>\u5806\u5185\u5b58<\/strong> \u4e2d\uff0c<strong>\u5bf9\u8c61\u7684\u5f15\u7528\u662f\u5185\u5b58\u5730\u5740<\/strong>\uff0c\u4f46\u8fd9\u4e9b\u5730\u5740\u5728\u4e0d\u540c\u7684 JVM \u6216\u8ba1\u7b97\u673a\u4e0a\u90fd\u662f<strong>\u65e0\u6548\u7684<\/strong>\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a <code>User user1 = new User(\"\u5f20\u4e09\", 25); User user2 = user1;<\/code> <code>user2<\/code> <strong>\u53ea\u662f <code>user1<\/code> \u7684\u5f15\u7528\uff08\u6307\u9488\uff09<\/strong>\uff0c\u5b83\u4eec\u6307\u5411 <strong>\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740<\/strong>\uff0c\u4f46\u8fd9\u4e2a\u5730\u5740<strong>\u5728\u4e0d\u540c\u673a\u5668\u4e0a\u662f\u65e0\u6548\u7684<\/strong>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u76f4\u63a5\u628a Java \u5185\u5b58\u5730\u5740\u4fdd\u5b58\u5230\u6587\u4ef6\u91cc\uff0c\u4e0b\u6b21\u7a0b\u5e8f\u91cd\u542f\uff0c\u5730\u5740\u4f1a\u53d8\u6210\u65e0\u6548\u7684\uff0c\u6240\u4ee5<strong>\u5fc5\u987b\u7528\u5e8f\u5217\u5316\u628a\u5bf9\u8c61\u8f6c\u6362\u6210\u53ef\u5b58\u50a8\u7684\u6570\u636e\u683c\u5f0f<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. Java \u5bf9\u8c61\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java \u7684\u5bf9\u8c61\u4e0d\u53ea\u662f\u7b80\u5355\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u8fd8\u53ef\u80fd\u5305\u542b\uff1a<ul><li><strong>\u96c6\u5408<\/strong>\uff08List\u3001Map\uff09<\/li><li><strong>\u7ee7\u627f\u5173\u7cfb<\/strong><\/li><li><strong>\u5faa\u73af\u5f15\u7528<\/strong><\/li><li><strong>\u81ea\u5b9a\u4e49\u7c7b<\/strong><\/li><\/ul>\u5982\u679c\u6ca1\u6709\u5e8f\u5217\u5316\u89c4\u5219\uff0c<strong>\u5b58\u50a8\u8fd9\u4e9b\u590d\u6742\u7ed3\u6784\u5c31\u4f1a\u975e\u5e38\u56f0\u96be<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u4e0b\u9762\u7684 <code>User<\/code> \u5bf9\u8c61\u91cc\u6709\u4e00\u4e2a <code>Address<\/code> \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Address {\n    String city;\n}\n\nclass User {\n    String name;\n    Address address;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u76f4\u63a5\u628a <code>User<\/code> \u4f5c\u4e3a\u4e8c\u8fdb\u5236\u5b58\u50a8\uff0c\u5b83\u7684 <code>address<\/code> \u53ef\u80fd\u662f\u4e00\u4e2a<strong>\u5185\u5b58\u5730\u5740<\/strong>\uff0c\u65e0\u6cd5\u6062\u590d\u6210\u539f\u59cb\u5bf9\u8c61\u3002<br>\u4f46\u662f\uff0c<strong>\u5e8f\u5217\u5316\u53ef\u4ee5\u628a <code>address<\/code> \u53d8\u6210\u6807\u51c6\u6570\u636e\u683c\u5f0f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"\u5f20\u4e09\",\n    \"address\": {\n        \"city\": \"\u5317\u4eac\"\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\uff0c<strong>\u4e0d\u7ba1\u5728\u54ea\u53f0\u673a\u5668\u4e0a\u90fd\u53ef\u4ee5\u6b63\u786e\u53cd\u5e8f\u5217\u5316<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4. \u5e8f\u5217\u5316\u8ba9 Java \u5bf9\u8c61\u53ef\u4ee5\u8de8\u8bed\u8a00\u3001\u8de8\u5e73\u53f0\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6ca1\u6709\u5e8f\u5217\u5316\uff0c\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4<strong>\u65e0\u6cd5\u76f4\u63a5\u7406\u89e3 Java \u5bf9\u8c61<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Java \u670d\u52a1\u5668<\/strong> \u9700\u8981\u628a <code>User<\/code> \u53d1\u9001\u7ed9 <strong>Python \u5ba2\u6237\u7aef<\/strong>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6ca1\u6709\u5e8f\u5217\u5316\uff0cJava \u53ea\u80fd\u4f20\u9012<strong>\u4e8c\u8fdb\u5236\u7684 JVM \u5bf9\u8c61<\/strong>\uff0cPython \u65e0\u6cd5\u7406\u89e3\u3002<\/li>\n\n\n\n<li>\u4f46\u5982\u679c\u7528 <strong>JSON \u6216 Protobuf<\/strong> \u8fdb\u884c\u5e8f\u5217\u5316\uff0cPython \u5c31\u80fd\u89e3\u6790\uff1a\n<ul class=\"wp-block-list\">\n<li>Java <strong>\u5e8f\u5217\u5316 JSON<\/strong>\uff1a <code>{\"name\": \"\u5f20\u4e09\", \"age\": 25}<\/code><\/li>\n\n\n\n<li>Python <strong>\u53cd\u5e8f\u5217\u5316 JSON<\/strong>\uff1a <code>import json data = '{\"name\": \"\u5f20\u4e09\", \"age\": 25}' user = json.loads(data) print(user[\"name\"]) # \u5f20\u4e09<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u8bba<\/strong>\uff1a\u5e8f\u5217\u5316\u8ba9 Java \u5bf9\u8c61\u53ef\u4ee5\u8de8\u5e73\u53f0\u4f7f\u7528\uff0c\u4e0d\u53d7 JVM \u9650\u5236\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u95ee\u9898<\/strong><\/th><th><strong>\u89e3\u91ca<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Java \u5bf9\u8c61\u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u5e8f\u5217\u5316\uff1f<\/strong><\/td><td>Java \u5185\u5b58\u5bf9\u8c61\u7684\u683c\u5f0f\u662f JVM \u4e13\u7528\u7684\uff0c\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u6216\u4f20\u8f93\u3002<\/td><\/tr><tr><td><strong>\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u5b58\u5bf9\u8c61\u7684\u4e8c\u8fdb\u5236\uff1f<\/strong><\/td><td>\u56e0\u4e3a Java \u5bf9\u8c61\u5305\u542b <strong>\u6307\u9488\u3001\u5185\u5b58\u5730\u5740\u3001\u65b9\u6cd5\u8868<\/strong>\uff0c\u4e0d\u540c JVM \u4e0d\u517c\u5bb9\u3002<\/td><\/tr><tr><td><strong>\u4e3a\u4ec0\u4e48 JSON \/ XML \/ Protobuf \u66f4\u597d\uff1f<\/strong><\/td><td>\u56e0\u4e3a\u5b83\u4eec\u662f <strong>\u6807\u51c6\u683c\u5f0f<\/strong>\uff0c\u53ef\u4ee5\u8de8\u8bed\u8a00\u3001\u8de8\u5e73\u53f0\u5b58\u50a8\u548c\u4f20\u8f93\u3002<\/td><\/tr><tr><td><strong>\u5e8f\u5217\u5316\u7684\u597d\u5904\uff1f<\/strong><\/td><td>\u8ba9\u5bf9\u8c61\u53ef\u4ee5\u5b58\u5165\u6570\u636e\u5e93\u3001\u6587\u4ef6\u3001\u7f51\u7edc\u4f20\u8f93\uff0c\u5e76\u80fd\u6062\u590d\u56de\u6765\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c<strong>\u5e8f\u5217\u5316\u7684\u672c\u8d28\u5c31\u662f\u628a JVM \u91cc\u7684\u201c\u5185\u5b58\u5bf9\u8c61\u201d\u8f6c\u6362\u6210\u201c\u53ef\u5b58\u50a8\u548c\u4f20\u8f93\u7684\u6807\u51c6\u683c\u5f0f\u201d<\/strong>\uff0c\u4ee5\u4fbf\u6570\u636e\u6301\u4e45\u5316\u548c\u8de8\u5e73\u53f0\u4f7f\u7528\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5e8f\u5217\u5316\uff08Serialization\uff09\uff1f \u5e8f\u5217\u5316\uff08Serialization\uff09 \u662f\u6307\u628a\u5bf9\u8c61\u8f6c\u6362\u6210\u53ef\u4ee5\u5b58\u50a8\u6216 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-java","category-3"],"_links":{"self":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":3,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts\/599\/revisions\/602"}],"wp:attachment":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}