PHP json_decode,包含长字符串和特殊字符

时间:2016-01-25 18:52:40

标签: php json regex string

我从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

1 个答案:

答案 0 :(得分:3)

您提供的print_r输出的解码JSON存在问题:键 iFlag 在同一对象中出现两次。这不应该是可能的。

您解码的原始JSON字符串将如下所示:

$json = '[
    {
        "id": 1535497,
        "tid": 6970000,
        "text": "Hello :) error is back! It\'s quite annoying!",
        "dFlag": 1,
        "iFlag": null,
        "rFlag": null,
        "member": null,
        "contact": {
            "id": 187,
            "name": "User Name",
            "_info": {
                "contact_href": "https:\/\/url\/187"
            }
        },
        "iFlag": null,
        "cFlag": null,
        "processNotifications": null,
        "dateCreated": "2015-12-08T13:59:19Z",
        "createdBy": "User Name user@domain.com (email)",
        "eFlag": 1,
        "_info": {
            "lastUpdated": "2015-12-08T13:59:19Z",
            "updatedBy": "User Name user@domain.com (email)"
        }
    }
]';

事实上,如果我像这样解码和打印它:

$data = json_decode($json, true); // translate objects to associative arrays
print_r ($data);

...输出与您在问题中提供的输出相同,但重复的 iFlag 属性除外:

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) [eFlag] => 1 [_info] => Array ( [lastUpdated] => 2015-12-08T13:59:19Z [updatedBy] => User Name user@domain.com (email) ) ) )

阵列美化打破了这个,只是意味着这个美化不够好。它试图解释print_r的输出,但显然它有一些弱点。然而,这并不表示JSON编码/解码存在问题。

print_r实际上在“美化”输出本身方面做得很好,但在浏览器上下文中,您需要保留格式以查看输出,例如通过将该输出包装在pre标记中。如果你这样做,你会看到这个输出:

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)
            [eFlag] => 1
            [_info] => Array
                (
                    [lastUpdated] => 2015-12-08T13:59:19Z
                    [updatedBy] => User Name user@domain.com (email)
                )

        )

)

现在我们可以再次对这些数据进行JSON编码:

$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;

输出是原始JSON,但重复的对象成员除外。没有其他问题:

[
    {
        "id": 1535497,
        "tid": 6970000,
        "text": "Hello :) error is back! It's quite annoying!",
        "dFlag": 1,
        "iFlag": null,
        "rFlag": null,
        "member": null,
        "contact": {
            "id": 187,
            "name": "User Name",
            "_info": {
                "contact_href": "https:\/\/url\/187"
            }
        },
        "cFlag": null,
        "processNotifications": null,
        "dateCreated": "2015-12-08T13:59:19Z",
        "createdBy": "User Name user@domain.com (email)",
        "eFlag": 1,
        "_info": {
            "lastUpdated": "2015-12-08T13:59:19Z",
            "updatedBy": "User Name user@domain.com (email)"
        }
    }
]

现在你可以再次解码:

$back = json_decode($json);
print_r ($back);

输出再次是您提供的原始print_r输出,但重复成员除外。所以JSON编码没有任何问题。