【发布时间】:2015-03-19 08:33:27
【问题描述】:
我向我的控制器发送一个包含 & 的字符串。当我执行 json_decode 时,它会在该字符串中返回 Disallowed Key Characters。
例如:
"1204" : {
"phone" : {
"0" : "666666"
},
"idcontact" : "1211",
"firstName" : "User1",
"address" : {
"0" : {
"State" : "Madrid",
"Country" : "Spain",
"City" : "Madrid"
}
},
"urls" : {
},
"company" : "Restaurant & Cafe Bar",
"birthday" : "1987\/03\/09",
"email" : {
"0" : "user@restaurant.com"
},
"job" : "Owner",
"lastName" : "LastName"}
它返回..
Disallowed Key Characters: Cafe Bar",
____"birthday" : "1987\/03\/09",
____"email" : {
______"0" : "user@restaurant.com"
____},
____"job" : "Owner",
____"lastName" : "LastName"}
我尝试更改函数 _clean_input_keys($str) 但我不知道该怎么做。
我该如何解决?
【问题讨论】:
-
Encode你的字符串 -
是encode,所以需要解码
-
因为 & 符号不是 JSON 中允许的字符。
urlencode()你的字符串在转换成 JSON 之前。 -
我尝试验证您的示例字符串:JSONLint - The JSON Validator。这是无效的。如果编辑为有效的 JSON,通过用大括号括起来,那么它在 PHP 中解码得很好。 '&' 在 JSON 中完全有效,并在 PHP (5.3.29) 中正确解码。无需对其进行转义或编码。我将您的 JSON 放入“nowdoc”中。
标签: php json codeigniter