Package com.alibaba.fastjson2
Class JSONPObject
java.lang.Object
com.alibaba.fastjson2.JSONPObject
JSONPObject is used to represent JSONP (JSON with Padding) data structure.
JSONP is a technique for safely requesting data from another domain. It wraps JSON data in a function call to bypass the same-origin policy that prevents direct access to resources from different domains.
Example usage:
// Create a JSONP object
JSONPObject jsonp = new JSONPObject("callback");
jsonp.addParameter(new JSONObject().fluentPut("id", 1).fluentPut("name", "test"));
// Serialize to JSONP string
String jsonpString = jsonp.toString(); // "callback({\"id\":1,\"name\":\"test\"})"
// Parse from JSONP string
JSONPObject parsed = JSON.parseObject(jsonpString, JSONPObject.class);
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for JSONPObjectJSONPObject(String function) Constructor with function name -
Method Summary
Modifier and TypeMethodDescriptionvoidaddParameter(Object parameter) Adds a parameter to this JSONP objectGets the function name of this JSONP objectGets the parameters list of this JSONP objectvoidsetFunction(String function) Sets the function name of this JSONP objecttoString()Serializes this JSONP object to JSONP string format
-
Constructor Details
-
JSONPObject
public JSONPObject()Default constructor for JSONPObject -
JSONPObject
Constructor with function name- Parameters:
function- the JSONP callback function name
-
-
Method Details
-
getFunction
Gets the function name of this JSONP object- Returns:
- the function name
-
setFunction
Sets the function name of this JSONP object- Parameters:
function- the function name to set
-
getParameters
Gets the parameters list of this JSONP object- Returns:
- the parameters list
-
addParameter
Adds a parameter to this JSONP object- Parameters:
parameter- the parameter to add
-
toString
Serializes this JSONP object to JSONP string format
-