[Java] JSONObject learning

Introduction

  JSONObject just a data structure, the data structure can be understood as JSON format ( Key-value  structure), a method may be used to put the object json additive element. JSONObject can be easily converted to a string, can be easily converted into other objects JSONObject objects.

  JSON (JavaScript Object Notation first letter acronym) is a lightweight data-interchange format, most often used for client - server communication. It is easy to read / write, and nothing to do with language. JSON JSON value may be another object, an array, numbers, strings, Boolean (true / false) or null. A rear end of a class is often developed to be contacted, the data to be encapsulated into json then passed to the distal end.

rely

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.46</version>
</dependency>

JSONObject interface

public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {

1.JSONObject inherited from JSON, JSON is a major class of Fastjson, often need to call the JSON two methods: toJSONString (Object) [the specified object serialized Json representation] and parseObject (String, Class) [the anti-json sequence into a specified pattern] Class
2.JSONObject achieved Map <String, Object>, seen JSONObject Map is a type of data structure, Map interface provides a method of operating a number of map commonly used CRUD.
3.JSONObject also achieved Cloneable, Serializable, InvocationHandler, described JSONObject supports copy (clone and JSONObject rewrite method), support serialization and deserialization, of InvocationHandler is achieved through a proxy instance zero call processing program interface, i.e. tag use Java dynamic proxy mechanism. JDK documentation indicates: Call handler has an associated instance of each agent. When a method is invoked on a proxy instance, calling a method of coding and its dispatch invoke method call handler.

JSONObject field

    private static final long         serialVersionUID         = 1L;
    private static final int          DEFAULT_INITIAL_CAPACITY = 16;

    private final Map<String, Object> map;

JSONObject has three fields
1.serialVersionUID (version identification) is a Java version identification for each sequence generated class can be used to ensure that during deserialization, sent by the sender and receiver are compatible recipient objects. If the throw InvalidClassException serialVersionUID recipient receives class serialVersionUID sent by the sender and inconsistent anti-sequence. Serialized serialVersionUID explicitly declared class values, as follows:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 1L;

When the value of serialVersionUID explicitly defined, according to various aspects of Java classes (specific reference Java serialization specification) dynamically generates a default serialVersionUID. Despite this, it is recommended that you explicitly specify the value of serialVersionUID of a serialized in every class, because different jdk compiler is likely to generate a different serialVersionUID default value, which led to InvalidClassExceptions throws an exception when deserializing. Therefore, in order to ensure that different compilers implement jdk, which is also consistent serialVersionUID value, the class must be serializable explicitly specify the serialVersionUID value. In addition, the best serialVersionUID modifier is private, because the serialVersionUID can not be inherited, it is recommended that the use of private modification serialVersionUID. From the above java.io.Serializable doc [documentation]
2.DEFAULT_INITIAL_CAPACITY default initial capacity, a size of 16
3.map , a container for the storage, the JSONObject two common types, a LinkedHashMap (ordered) and HashMap type . The default initial size is 16.

JSONObject constructor

 The most important constructor is public JSONObject (int initialCapacity, boolean ordered ), other constructors call this constructor directly. initialCapacity initial capacity, ordered for the map if ordered

// configured capacity of 16 units HashMap as the JSONObject 
public the JSONObject () {
     // call the JSONObject public (int initialCapacity, Boolean ordered) 
    the this (DEFAULT_INITIAL_CAPACITY, to false ); 
} 
// construct custom Map <String, Object> container type of the JSONObject 
public the JSONObject (the Map <String, Object> Map) {
     IF (Map == null ) {
         the throw  new new an IllegalArgumentException ( "Map iS null." ); 
    } 
    the this .map = Map; 
} 
// configured capacity of 16 units JSONObject, choose whether the vessel ordered: a LinkedHashMap (to true), the HashMap (to false) 
public JSONObject(boolean ordered){
    this(DEFAULT_INITIAL_CAPACITY, ordered);
}
//构造自定义初始大小的HashMap类型的JSONObject
public JSONObject(int initialCapacity){
    this(initialCapacity, false);
}

public JSONObject(int initialCapacity, boolean ordered){
    if (ordered) {
        map = new LinkedHashMap<String, Object>(initialCapacity);
    } else {
        map = new HashMap<String, Object>(initialCapacity);
    }
}

Use Case

// configured capacity of 16 units HashMap as the JSONObject 
the JSONObject jsonObject = new new the JSONObject (); 

// configured capacity of 16 units JSONObject, optionally vessel orderly manner: a LinkedHashMap (to true), HashMap (to false) 
the JSONObject jsonObject1 = new new the JSONObject ( to true ); 

 // construct custom Map <String, Object> type of container the JSONObject 
Map <String, Object> = Map new new of ConcurrentHashMap <> (16 ); 
the JSONObject jsonObject2 = new new the JSONObject (Map); 

// custom 
= jsonObject3 the JSONObject new new the JSONObject (32, to false );

JSONObject method

JSONObject JSON but did not override inherited methods listed below temporarily.

 

 

 

 Original: https://blog.csdn.net/Butterfly_resting/article/details/100053544

Guess you like

Origin www.cnblogs.com/jxd283465/p/11803510.html