是否可以在URL中传递JSON?
例如,我有一个数组:
data = {
"name": "test",
"user_id": 1
}
我想将其传递到以下网址:
http://example.com/jsonArray
答案 0 :(得分:1)
你应该更好地使用POST传递这种类型的数据,但是,你绝对可以做一个:
$str = serialize(json_decode($data, true));
然后在网址中传递$ str。
答案 1 :(得分:0)
您无法直接发送JSON数组,但您可以通过允许将其发送到API来准备它。您将JSON转换为字符串并对其进行编码,以便为GET正确格式化。
// your JSON data
var data = '{"name":"Homer Simpson","status":"Suspended","disciplinary-dates":["2018-01-2","2018-02-14","2018-03-17"]}' ;
// take your JSON, turn it into a string and then encode it
var urlParams = encodeURIComponent(JSON.stringify(data)) ;
// your URL adding your JSON data as the value
var url = 'http://example.com/?params=' + urlParams ;
在PHP端,您将解码
// note: this code does not have any error checking - you should add it
// get your params
$params = $_GET['params'] ;
// decode the string
$decodedParams = urldecode($params) ;
// turn your string into an array
$wasJSONAsArray = json_decode($decodedParams, true) ;
// turn your string into a std object
$wasJSONAsStdObj = json_decode($decodedParams) ;