国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Java Javagetting Started Learn more about the transient keyword in Java

Learn more about the transient keyword in Java

Nov 27, 2019 pm 04:28 PM
java transient Keywords

Learn more about the transient keyword in Java

The transient keyword may be very unfamiliar and has not been used much, but the transient keyword plays an important role in java. Missing status!

In the process of learning javatransientThe reason why the keyword is rare is actually inseparable from its function: The main function of the transientkeyword is to make some Member attribute variables modified by the transient keyword are not serialized. In fact, it is precisely for this reason that serialization operations are rarely used in the learning process, usually in actual development! As for serialization, I believe that many novices have been confused or have no specific concept. This article will introduce it below.

1. What is serialization?

Speaking of serialization, another concept that comes with it is deserialization. Don’t panic, little white children’s shoes. Remembering serialization is equivalent to remembering deserialization. , because deserialization is the reverse of serialization, so the blogger recommends just remembering the concept of serialization to avoid confusing yourself.

Serialization defined by professional terms:

Java provides a mechanism for object serialization. An object can be represented by a byte sequence that contains information such as the object's data, the object's type, and the attributes stored in the object. After the byte sequence is written to the file, it is equivalent to persisting the information of an object in the file. Conversely, the byte sequence can be read back from the file, the object reconstructed, and deserialized. The object's data, the object's type, and the data information stored in the object can all be used to create objects in memory.

Yichun’s term definition Serialization:

Serialization: Byte——> Object

Actually, what I summarized is the above conclusion. If you don’t understand, just refer to the definition of professional terms. Once you understand, just remember my words. If you can’t remember, please beat me to death (I’m just kicking my m He is a genius)

Diagram understanding serialization:
Learn more about the transient keyword in Java
What? You don’t understand what a byte is? In fact, I have already introduced serialization in an article about IO flow. Don’t worry, it’s absolutely very detailed. Just look at the name of the article and you’ll know it.

The most sassy, ??comprehensive and detailed IO flow tutorial in history. Even novices can understand it!

2. Why serialization?

The concept of serialization was mentioned in the previous section. After knowing the concept, we must know why we need to serialize.

Before talking about the reasons why serialization is necessary, let me give you a chestnut as a blogger:

Just like when you go to buy food on the street, the general operation is to package it in plastic bags. When I get home and want to cook, I take out the dishes. And this series of operations is like serialization and deserialization!

Serialization of objects in Java refers to converting objects into representations in the form of byte sequences. These byte sequences contain the data and information of the object, a serialized object can be written to a database or file , and can also be used for network transmission , generally when we use cache cache (Insufficient memory space may result in local storage to the hard disk) or When calling rpc remotely (network transmission), we often need to implement our entity classesSerializableInterface, the purpose is to make it serializable.

●In the development processTo use the transientkeyword modified chestnut:

If a user has some password and other information, For security reasons, if you do not want to be transmitted during network operations, the variables corresponding to this information can be added with the transient keyword. In other words, the life cycle of this field only exists in the caller's memory and will not be written to disk for persistence.

●In the development processNo need for transientKeyword modified chestnuts:

1. Field values ??in the class can be deduced based on other fields come out.
2. Depending on the specific business requirements, which fields do not want to be serialized;

I wonder if you have ever thought about why they should not be serialized? In fact, it is mainly to save storage space. Optimize the program!

PS: I remember when I looked at the HashMap source code before, I found that a field was modified with transient. I think it makes sense. There is really no need to modify this The modCount field is serialized because it is meaningless. modCount is mainly used to determine whether the HashMap has been modified (like during put and remove operations, modCount will increase by itself). For this kind of variable, it can be any value at the beginning. The value, of course, is 0 (it will be 0 when it is new, deserialized, or cloned), and there is no need to persist its value.

Of course, the ultimate goal after serialization is to deserialize and restore it to the original Java object. Otherwise, what else would you do after serialization? Just like buying groceries, wrap the last part in a plastic bag. It is still for the convenience and safety of getting home before removing the plastic bag, so the serialized byte sequence can be restored to a Java object. This process is deserialization.

3. The use of serialization and transient

1. The class of the object that needs to be serialized must Implement serialization interface: Java.lang.Serializable interface (a flag interface without any abstract methods). Most classes in Java implement this interface, such as: String,Integer classes, etc., classes that do not implement this interface will not serialize or deserialize any state, and will throw NotSerializableException exception.

2. The bottom layer will judge that if the current object is an instance of Serializable, serialization is allowed, and the Java object instanceof Serializable will be used to judge.

3, Use object streamObjectOutputStream in Java to complete serialization and ObjectInputStreamstream deserialization   

==ObjectOutputStream: Serialization operation through writeObject() method==

==ObjectInputStream: Deserialization operation through readObject() method==

4. All properties of this class must be serializable. If there is an attribute that does not need to be serializable, the attribute must be marked as transient and modified with the transient keyword.
Learn more about the transient keyword in Java
Because of the bytes, the operation of the stream must be involved, that is, the object stream is also called the serialization stream ObjectOutputstream. Let’s analyze the serialization operation code in various situations!

Here, I really strongly recommend readers who read the Yichun blog, please try to knock it, remember to take it with you at a glance or copy it and run it, especially for little white children's shoes, believe me ! You will definitely gain something different. Don’t feel like it’s a waste of time, sometimes slow is faster, Yichun experienced it firsthand!

3.1. The Serializable interface is not implemented for serialization

package TransientTest;
import java.io.*;

class UserInfo {  //================================注意這里沒有實(shí)現(xiàn)Serializable接口
    private String name;
    private transient String password;

    public UserInfo(String name,String psw) {
        this.name = name;
        this.password=psw;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

public class TransientDemo {
    public static void main(String[] args) {

        UserInfo userInfo=new UserInfo("老王","123");
        System.out.println("序列化之前信息:"+userInfo);

        try {
            ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("userinfo.txt"));
            output.writeObject(new UserInfo("老王","123"));
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Running result

Learn more about the transient keyword in Java

3.2. Serialization of the Serializable interface

When we add the Serializable interface and run it again, we will find that the content of the userinfo.txt file that appears in the project is as follows:

Learn more about the transient keyword in Java

In fact, this is not the point. The point is that the serialization operation was successful!

3.3. Ordinary serialization situation

package TransientTest;
import java.io.*;

class UserInfo implements Serializable{  //第一步實(shí)現(xiàn)Serializable接口
    private String name;
    private String password;//都是普通屬性==============================

    public UserInfo(String name,String psw) {
        this.name = name;
        this.password=psw;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

public class TransientDemo {
    public static void main(String[] args) throws ClassNotFoundException {

        UserInfo userInfo=new UserInfo("程序員老王","123");
        System.out.println("序列化之前信息:"+userInfo);

        try {
            ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("userinfo.txt")); //第二步開始序列化操作
            output.writeObject(new UserInfo("程序員老王","123"));
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            ObjectInputStream input=new ObjectInputStream(new FileInputStream("userinfo.txt"));//第三步開始反序列化操作
            Object o = input.readObject();//ObjectInputStream的readObject方法會(huì)拋出ClassNotFoundException
            System.out.println("序列化之后信息:"+o);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Running results:

序列化之前信息:UserInfo{name='程序員老王', password='123'}
序列化之后信息:UserInfo{name='程序員老王', password='123'}

3.4. Transient serialization situation

package TransientTest;
import java.io.*;

class UserInfo implements Serializable{  //第一步實(shí)現(xiàn)Serializable接口
    private String name;
    private transient String password; //特別注意:屬性由transient關(guān)鍵字修飾===========

    public UserInfo(String name,String psw) {
        this.name = name;
        this.password=psw;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

public class TransientDemo {
    public static void main(String[] args) throws ClassNotFoundException {

        UserInfo userInfo=new UserInfo("程序員老王","123");
        System.out.println("序列化之前信息:"+userInfo);

        try {
            ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("userinfo.txt")); //第二步開始序列化操作
            output.writeObject(new UserInfo("程序員老王","123"));
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            ObjectInputStream input=new ObjectInputStream(new FileInputStream("userinfo.txt"));//第三步開始反序列化操作
            Object o = input.readObject();//ObjectInputStream的readObject方法會(huì)拋出ClassNotFoundException
            System.out.println("序列化之后信息:"+o);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Running result:

序列化之前信息:UserInfo{name='程序員老王', password='123'}
序列化之后信息:UserInfo{name='程序員老王', password='null'}

Pay special attention to the result, add the transient modified attribute value to the default valuenull! If the attribute modified by transient is of type int, then its value must be 0 after being serialized. Of course, you can try it. What does this mean? It means that attributes marked as transient will not be saved when the object is serialized (or the variable will not be persisted)

3.5, static serialization situation

package TransientTest;
import java.io.*;

class UserInfo implements Serializable{  //第一步實(shí)現(xiàn)Serializable接口
    private String name;
    private static String password; //特別注意:屬性由static關(guān)鍵字修飾==============

    public UserInfo(String name, String psw) {
        this.name = name;
        this.password=psw;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

public class TransientDemo {
    public static void main(String[] args) throws ClassNotFoundException {

        UserInfo userInfo=new UserInfo("程序員老王","123");
        System.out.println("序列化之前信息:"+userInfo);

        try {
            ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("userinfo.txt")); //第二步開始序列化操作
            output.writeObject(new UserInfo("程序員老王","123"));
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            ObjectInputStream input=new ObjectInputStream(new FileInputStream("userinfo.txt"));//第三步開始反序列化操作
            Object o = input.readObject();//ObjectInputStream的readObject方法會(huì)拋出ClassNotFoundException
            System.out.println("序列化之后信息:"+o);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Run result:

序列化之前信息:UserInfo{name='程序員老王', password='123'}
序列化之后信息:UserInfo{name='程序員老王', password='123'}

At this time, you will mistakenly think that the static modification has also been serialized. In fact, it is not the case. In fact, it is easy to get confused here! Obviously taking out null (default value) can indicate that it will not be serialized. It is clearly not changed to the default value here, why do you still say that static will not be serialized?

In fact, the value of the static variable name in the class after deserialization is actually the value of the corresponding static variable in the current JVM. This value is in the JVM and is not deserialized. derived. That is to say, variables modified by static do not participate in serialization! But we can’t say it without proof, yes, then let’s compare the two programs and we will understand!

The first program: This is a name attribute program that has not been modified by static:

package Thread;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class UserInfo implements Serializable {
    private String name;
    private transient String psw;

    public UserInfo(String name, String psw) {
        this.name = name;
        this.psw = psw;
    }

    public  String getName() {
        return name;
    }

    public  void setName(String name) {
        this.name = name;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }

    public String toString() {
        return "name=" + name + ", psw=" + psw;
    }
}
public class TestTransient {
    public static void main(String[] args) {
        UserInfo userInfo = new UserInfo("程序員老過", "456");
        System.out.println(userInfo);
        try {
            // 序列化,被設(shè)置為transient的屬性沒有被序列化
            ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("UserInfo.txt"));
            o.writeObject(userInfo);
            o.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try {
            //在反序列化之前改變name的值 =================================注意這里的代碼
            userInfo.setName("程序員老改");
            // 重新讀取內(nèi)容
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("UserInfo.txt"));
            UserInfo readUserInfo = (UserInfo) in.readObject();
            //讀取后psw的內(nèi)容為null
            System.out.println(readUserInfo.toString());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

Running results:

name=程序員老過, psw=456
name=程序員老過, psw=null

It can be seen from the program running results that in Before deserialization, I tried to change the value of name to the programmer, but it was unsuccessful!

第二個(gè)程序:這是一個(gè)被static修飾的name屬性程序:

package Thread;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class UserInfo implements Serializable {
    private static final long serialVersionUID = 996890129747019948L;
    private static String name;
    private transient String psw;

    public UserInfo(String name, String psw) {
        this.name = name;
        this.psw = psw;
    }

    public  String getName() {
        return name;
    }

    public  void setName(String name) {
        this.name = name;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }

    public String toString() {
        return "name=" + name + ", psw=" + psw;
    }
}
public class TestTransient {
    public static void main(String[] args) {
        UserInfo userInfo = new UserInfo("程序員老過", "456");
        System.out.println(userInfo);
        try {
            // 序列化,被設(shè)置為transient的屬性沒有被序列化
            ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("UserInfo.txt"));
            o.writeObject(userInfo);
            o.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try {
            //在反序列化之前改變name的值
            userInfo.setName("程序員老改");
            // 重新讀取內(nèi)容
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("UserInfo.txt"));
            UserInfo readUserInfo = (UserInfo) in.readObject();
            //讀取后psw的內(nèi)容為null
            System.out.println(readUserInfo.toString());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

運(yùn)行結(jié)果:

name=程序員老過, psw=456
name=程序員老改, psw=null

從程序運(yùn)行結(jié)果中可以看出,在反序列化之前試著改變name的值為程序員老改,結(jié)果是成功的!現(xiàn)在對(duì)比一下兩個(gè)程序是不是就很清晰了?

static關(guān)鍵字修飾的成員屬性優(yōu)于非靜態(tài)成員屬性加載到內(nèi)存中,同時(shí)靜態(tài)也優(yōu)于對(duì)象進(jìn)入到內(nèi)存中,被static修飾的成員變量不能被序列化,序列化的都是對(duì)象,靜態(tài)變量不是對(duì)象狀態(tài)的一部分,因此它不參與序列化。所以將靜態(tài)變量聲明為transient變量是沒有用處的。因此,反序列化后類中static型變量name的值實(shí)際上是當(dāng)前JVM中對(duì)應(yīng)static變量的值,這個(gè)值是JVM中的并不是反序列化得出的。

如果對(duì)static關(guān)鍵字還是不太清楚理解的童鞋可以參考這篇文章,應(yīng)該算是不錯(cuò)的:深入理解static關(guān)鍵字

3.6、final序列化情況

對(duì)于final關(guān)鍵字來講,final變量將直接通過值參與序列化,至于代碼程序我就不再貼出來了,大家可以試著用final修飾驗(yàn)證一下!

主要注意的是final 和transient可以同時(shí)修飾同一個(gè)變量,結(jié)果也是一樣的,對(duì)transient沒有影響,這里主要提一下,希望各位以后在開發(fā)中遇到這些情況不會(huì)滿頭霧水!

4、java類中serialVersionUID作用

既然提到了transient關(guān)鍵字就不得不提到序列化,既然提到了序列化,就不得不提到serialVersionUID了,它是啥呢?基本上有序列化就會(huì)存在這個(gè)serialVersionUID。

Learn more about the transient keyword in Java
serialVersionUID適用于Java的序列化機(jī)制。簡單來說,Java的序列化機(jī)制是通過判斷類的serialVersionUID來驗(yàn)證版本一致性的。在進(jìn)行反序列化時(shí),JVM會(huì)把傳來的字節(jié)流中的serialVersionUID與本地相應(yīng)實(shí)體類的serialVersionUID進(jìn)行比較,如果相同就認(rèn)為是一致的,可以進(jìn)行反序列化,否則就會(huì)出現(xiàn)序列化版本不一致的異常,即是InvalidCastException,在開發(fā)中有時(shí)候可寫可不寫,建議最好還是寫上比較好。

5、transient關(guān)鍵字小結(jié)

1、變量被transient修飾,變量將不會(huì)被序列化
2、transient關(guān)鍵字只能修飾變量,而不能修飾方法和類。
3、被static關(guān)鍵字修飾的變量不參與序列化,一個(gè)靜態(tài)static變量不管是否被transient修飾,均不能被序列化。
4、final變量值參與序列化,final transient同時(shí)修飾變量,final不會(huì)影響transient,一樣不會(huì)參與序列化

第二點(diǎn)需要注意的是:本地變量是不能被transient關(guān)鍵字修飾的。變量如果是用戶自定義類變量,則該類需要實(shí)現(xiàn)Serializable接口

第三點(diǎn)需要注意的是:反序列化后類中static型變量的值實(shí)際上是當(dāng)前JVM中對(duì)應(yīng)static變量的值,這個(gè)值是JVM中的并不是反序列化得出的。

結(jié)語:被transient關(guān)鍵字修飾導(dǎo)致不被序列化,其優(yōu)點(diǎn)是可以節(jié)省存儲(chǔ)空間。優(yōu)化程序!隨之而來的是會(huì)導(dǎo)致被transient修飾的字段會(huì)重新計(jì)算,初始化!

本文來自?java入門?欄目,歡迎學(xué)習(xí)!

The above is the detailed content of Learn more about the transient keyword in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to iterate over a Map in Java? How to iterate over a Map in Java? Jul 13, 2025 am 02:54 AM

There are three common methods to traverse Map in Java: 1. Use entrySet to obtain keys and values at the same time, which is suitable for most scenarios; 2. Use keySet or values to traverse keys or values respectively; 3. Use Java8's forEach to simplify the code structure. entrySet returns a Set set containing all key-value pairs, and each loop gets the Map.Entry object, suitable for frequent access to keys and values; if only keys or values are required, you can call keySet() or values() respectively, or you can get the value through map.get(key) when traversing the keys; Java 8 can use forEach((key,value)-&gt

Java Optional example Java Optional example Jul 12, 2025 am 02:55 AM

Optional can clearly express intentions and reduce code noise for null judgments. 1. Optional.ofNullable is a common way to deal with null objects. For example, when taking values ??from maps, orElse can be used to provide default values, so that the logic is clearer and concise; 2. Use chain calls maps to achieve nested values ??to safely avoid NPE, and automatically terminate if any link is null and return the default value; 3. Filter can be used for conditional filtering, and subsequent operations will continue to be performed only if the conditions are met, otherwise it will jump directly to orElse, which is suitable for lightweight business judgment; 4. It is not recommended to overuse Optional, such as basic types or simple logic, which will increase complexity, and some scenarios will directly return to nu.

Comparable vs Comparator in Java Comparable vs Comparator in Java Jul 13, 2025 am 02:31 AM

In Java, Comparable is used to define default sorting rules internally, and Comparator is used to define multiple sorting logic externally. 1.Comparable is an interface implemented by the class itself. It defines the natural order by rewriting the compareTo() method. It is suitable for classes with fixed and most commonly used sorting methods, such as String or Integer. 2. Comparator is an externally defined functional interface, implemented through the compare() method, suitable for situations where multiple sorting methods are required for the same class, the class source code cannot be modified, or the sorting logic is often changed. The difference between the two is that Comparable can only define a sorting logic and needs to modify the class itself, while Compar

How to fix java.io.NotSerializableException? How to fix java.io.NotSerializableException? Jul 12, 2025 am 03:07 AM

The core workaround for encountering java.io.NotSerializableException is to ensure that all classes that need to be serialized implement the Serializable interface and check the serialization support of nested objects. 1. Add implementsSerializable to the main class; 2. Ensure that the corresponding classes of custom fields in the class also implement Serializable; 3. Use transient to mark fields that do not need to be serialized; 4. Check the non-serialized types in collections or nested objects; 5. Check which class does not implement the interface; 6. Consider replacement design for classes that cannot be modified, such as saving key data or using serializable intermediate structures; 7. Consider modifying

How to handle character encoding issues in Java? How to handle character encoding issues in Java? Jul 13, 2025 am 02:46 AM

To deal with character encoding problems in Java, the key is to clearly specify the encoding used at each step. 1. Always specify encoding when reading and writing text, use InputStreamReader and OutputStreamWriter and pass in an explicit character set to avoid relying on system default encoding. 2. Make sure both ends are consistent when processing strings on the network boundary, set the correct Content-Type header and explicitly specify the encoding with the library. 3. Use String.getBytes() and newString(byte[]) with caution, and always manually specify StandardCharsets.UTF_8 to avoid data corruption caused by platform differences. In short, by

Java method references explained Java method references explained Jul 12, 2025 am 02:59 AM

Method reference is a way to simplify the writing of Lambda expressions in Java, making the code more concise. It is not a new syntax, but a shortcut to Lambda expressions introduced by Java 8, suitable for the context of functional interfaces. The core is to use existing methods directly as implementations of functional interfaces. For example, System.out::println is equivalent to s->System.out.println(s). There are four main forms of method reference: 1. Static method reference (ClassName::staticMethodName); 2. Instance method reference (binding to a specific object, instance::methodName); 3.

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

How to parse JSON in Java? How to parse JSON in Java? Jul 11, 2025 am 02:18 AM

There are three common ways to parse JSON in Java: use Jackson, Gson, or org.json. 1. Jackson is suitable for most projects, with good performance and comprehensive functions, and supports conversion and annotation mapping between objects and JSON strings; 2. Gson is more suitable for Android projects or lightweight needs, and is simple to use but slightly inferior in handling complex structures and high-performance scenarios; 3.org.json is suitable for simple tasks or small scripts, and is not recommended for large projects because of its lack of flexibility and type safety. The choice should be decided based on actual needs.

See all articles