【发布时间】:2016-01-25 18:52:40
【问题描述】:
我收到来自 API 服务器的 JSON 响应。响应的结果数量会有所不同,并且可能包含多个带有长字符串的键。这些长字符串也可能包含特殊字符。
这是我在 json_decode 之后的示例数组:
Array ( [0] => Array ( [id] => 1535497 [tid] => 6970000 [text] => Hello :) error is back! It's quite annoying! [dFlag] => 1 [iFlag] => [rFlag] => [member] => [contact] => Array ( [id] => 187 [name] => User Name [_info] => Array ( [contact_href] => https://url/187 ) ) [cFlag] => [processNotifications] => [dateCreated] => 2015-12-08T13:59:19Z [createdBy] => User Name user@domain.com (email) [iFlag] => [eFlag] => 1 [_info] => Array ( [lastUpdated] => 2015-12-08T13:59:19Z [updatedBy] => User Name user@domain.com (email) ) ) )
Array ( [1] => Array ( [id] => 1535499 [tid] => 6970000 [text] => Hello. Lorem Ipsum. Lorem ipsum. [dFlag] => 1 [iFlag] => [rFlag] => [member] => [contact] => Array ( [id] => 187 [name] => User Name [_info] => Array ( [contact_href] => https://url/187 ) ) [cFlag] => [processNotifications] => [dateCreated] => 2015-12-08T13:59:19Z [createdBy] => User Name user@domain.com (email) [iFlag] => [eFlag] => 1 [_info] => Array ( [lastUpdated] => 2015-12-08T13:59:19Z [updatedBy] => User Name user@domain.com (email) ) ) )
您可以在数组美化器中看到它在哪里中断:http://phillihp.com/toolz/php-array-beautifier/
例如,某些字符串包含':)',因此当我应用 json_decode($response,true) 时,由于字符串中包含右括号,转换后的数组会中断。
我认为这是一个可能的解决方案的唯一方法是使用单独的函数(下面的示例)和正则表达式查询,但这似乎是开销。来源:http://php.net/manual/en/function.json-encode.php
<?php
function json_clean_decode($json, $assoc = false, $depth = 512, $options = 0) {
// sample regex
$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
if(version_compare(phpversion(), '5.4.0', '>=')) {
$json = json_decode($json, $assoc, $depth, $options);
}
elseif(version_compare(phpversion(), '5.3.0', '>=')) {
$json = json_decode($json, $assoc, $depth);
} else {
$json = json_decode($json, $assoc);
}
return $json;
}
?>
我想必须有另一种方法来处理从 json 解码的长字符串和特殊字符。
TIA
【问题讨论】:
-
JSON 没有任何 cmets,如果你有,它是一个自定义协议而不是真正的 JSON
-
这只是一个示例函数。正则表达式可以更改为其他内容,即查找:)。我正在尝试寻找一种替代方法来处理这些字符串中的长字符串和括号。
-
真正有效的 json 可以包含任意顺序的任意字符。如果现在有什么东西破坏了你的数组(我什至无法想象如何)它不是一个 json。
-
你能不能先用add_slashes()到json,然后用json_decode?span>
-
请注意,我们正在查看
print_r的输出。不要指望输出遵守某些语法!它不是 JSON。相同的 JSON 表示完全没有问题,所有字符串都将被引用,避免对括号的任何误解。如果这里有什么问题,那就是这个美化器,没有别的。