json_encode()问题并在PHP中存储在cookie问题中

时间:2018-09-21 06:51:35

标签: php arrays json

当我转换json_encode时;它以正确的格式进行转换,但是当我将其存储在cookie中时;格式更改。我想按原样存储在cookie中。

JSON STRING:

{
"ID": "0",
  "basicAddress": {
    "ID": "0",
    "Line1": "327 S Main St",
    "Line2": "",
    "Line3": "",
    "City": "Fitzgerald",
    "ZipCode": "31750",
    "StateProv": "",
    "Country": "",
    "stateProv": "GA"
  },
  "Name": "",
  "Latitude": "31.7114886",
  "Longitude": "-83.25471970000001",
  "IsPreferred": "false"
}

在Cookie中存储以下内容:

%7B%22ID%22%3A%220%22%2C%22basicAddress%22%3A%7B%22ID%22%3A%220%22%2C%22Line1%22%3A%22327+S+Main+St%22%2C%22Line2%22%3A%22%22%2C%22Line3%22%3A%22%22%2C%22City%22%3A%22Fitzgerald%22%2C%22ZipCode%22%3A%2231750%22%2C%22StateProv%22%3A%22%22%2C%22Country%22%3A%22%22%2C%22stateProv%22%3A%22GA%22%7D%2C%22Name%22%3A%22%22%2C%22Latitude%22%3A%2231.7114886%22%2C%22Longitude%22%3A%22-83.25471970000001%22%2C%22IsPreferred%22%3A%22false%22%7D

enter image description here

2 个答案:

答案 0 :(得分:0)

setcookie对值进行URL编码是正确的行为。您可以使用setrawcookie来避免这种自动编码,但是接下来,您需要确保自己的Cookie格式正确以符合HTTP规范。

答案 1 :(得分:0)

我找到了解决方案,并且按我的要求它绝对正确

$address={
"ID": "0",
  "basicAddress": {
    "ID": "0",
    "Line1": "327 S Main St",
    "Line2": "",
    "Line3": "",
    "City": "Fitzgerald",
    "ZipCode": "31750",
    "StateProv": "",
    "Country": "",
    "stateProv": "GA"
  },
  "Name": "",
  "Latitude": "31.7114886",
  "Longitude": "-83.25471970000001",
  "IsPreferred": "false"
};
cookie_expire_time=30000;
header("Set-Cookie: address=$address; Domain=.example.com;Path=/; Max-Age=".cookie_expire_time.";");