代码之家  ›  专栏  ›  技术社区  ›  Matt

json_decode()返回空问题

  •  11
  • Matt  · 技术社区  · 16 年前

    我的JSON有问题。它在php 5.3中正确返回(所以我不能使用json_last_error()),当我将字符串显式复制到json_decode(json_decode('…')中时,它成功返回。它只在我将结果作为变量传递并且使用php 5.2时返回空值,这正是我需要的。

    输出来自phpunit中的json日志:

    [
        {
            "event": "suiteStart",
            "suite": "",
            "tests": 2
        },
        {
            "event": "suiteStart",
            "suite": "TagTestCase",
            "tests": 2
        },
        {
            "event": "test",
            "suite": "TagTestCase",
            "test": "TagTestCase::test_it",
            "status": "fail",
            "time": 0.00248718261719,
            "trace": [
                {
                    "file": "\/UnitTest\/PHPUnit.php",
                    "line": 98,
                    "function": "run",
                    "class": "PHPUnit_Framework_TestSuite",
                    "type": "->",
                    "args": [
                        {
    
                        }
                    ]
                },
                {
                    "file": "\/UnitTest\/PHPUnit.php",
                    "line": 116,
                    "function": "run",
                    "class": "PHPUnit",
                    "type": "->",
                    "args": [
    
                    ]
                },
                {
                    "file": "\/UnitTest\/PHPUnit.php",
                    "line": 212,
                    "function": "__tostring",
                    "class": "PHPUnit",
                    "type": "->",
                    "args": [
    
                    ]
                }
            ],
            "message": "false assertionzzzzz.\nFailed asserting that <boolean:false> is true."
        },
        {
            "event": "test",
            "suite": "TagTestCase",
            "test": "TagTestCase::test_two",
            "status": "pass",
            "time": 0.00182914733887,
            "trace": [
    
            ],
            "message": ""
        }
    ]
    

    编辑:这些是我一直在探索的路径-也许你是个更好的探险家。 有三种可能有帮助的途径:

    • php 5.2和5.3中的json_decode()有什么不同?他们改变了什么?
    • 其他人使用phpunit中的JSON,以及如何解析它。
    • 当你把它放在一个变量中时会发生什么变化,而不是把它打印到屏幕上并复制到json_decode()中

    任何帮助都将是巨大的!!)感激。

    谢谢! 马特

    9 回复  |  直到 9 年前
        1
  •  20
  •   Matt    16 年前

    多么可怕的调试会话啊……好吧,有个好消息……我想出来了……

    我开始用Ajax查看它并用Firebug记录它…结果发现json-decode(或者顺便说一下eval)无法处理 &quot; 这是普普尼特送回的(来吧,塞巴斯蒂安!)所以要修复它:

    $json = str_replace('&quot;', '"', $json);
    

    现在我觉得他们是一样的……也许有人能启发我……

        2
  •  17
  •   Matija    13 年前

    昨天,我花了2个小时检查并修复了这个错误,最后我发现在我想要解码的JSON字符串中有‘’斜杠。因此,合乎逻辑的做法是使用stripslashes函数或类似于不同pl的函数。

    当然,最好的方法是Sill打印出这个var,并在json_解码后看到它变成什么,如果它为空,您也可以使用 json_last_error() 函数来确定错误,它将返回整数,但下面介绍这些int:

    0=json_错误_无

    1=json_错误_深度

    2=json_错误_状态_不匹配

    3=json_错误_ctrl_char

    4=json_错误_语法

    5=json_错误\u utf8

    在我的例子中,我得到了作为数字的json_last_error()的输出。 四 就是这样 json_错误_语法 . 然后我去看看我想要转换的字符串,它在最后一行有:

    '\'title\' error ...'
    

    在那之后真的只是一个简单的解决办法。

    $json = json_decode(stripslashes($response));
    if (json_last_error() == 0) { // you've got an object in $json}
    
        3
  •  4
  •   Keith Minkler    16 年前

    当我使用:

    phpunit --log-json file.json <test_file>
    

    (使用phpunit 3.4.13),它创建的文件似乎不包含有效的JSON,

    json文件包含“json”,它看起来像:

    {...}{...}{...}{...}
    

    而不是我期望看到的:

    [{...},{...},{...},{...}]
    

    不确定是否与您看到的问题相同,问题中的示例JSON输出似乎比我看到的更有效。

    一旦添加缺少的逗号和括号,就可以使用php 5.2.10或php 5.3.2上的json_decode()对其进行解析。

        4
  •  2
  •   Samuel Kwame Antwi    10 年前

    Try this the problem is this html_entity_decode($your value);

     if(get_magic_quotes_gpc()){
       $param = stripslashes($row['your column name']);
     }else{
       $param = $row['your column name'];
    }
    
    
    $param = json_decode(html_entity_decode($param),true);
     $json_errors = array(
    JSON_ERROR_NONE => 'No error has occurred',
    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
    JSON_ERROR_SYNTAX => 'Syntax error',
    );
     echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
    print_r($param);
    
        5
  •  1
  •   Balibia    13 年前

    你应该用这个

    $jsonstring = stripslashes(str_replace('\"', '"', $jsonstring));
    

    我用php 5.3测试过这个

        6
  •  0
  •   shadow_of__soul    16 年前

    尝试设置所有的错误报告,json_decode()应该在转换失败的偏移位置给出一个通知。

        7
  •  0
  •   Carlton BlueStraggler    14 年前

    Sebastian关于JSON输出格式的有用文章

    https://github.com/sebastianbergmann/phpunit/issues/142

    使用Keith的建议将允许正确分析数据

        8
  •  0
  •   user3633397    12 年前

    我注意到PHP版本5.14.12的这种行为,它也可能适用于其他版本。
    当使用file_get_contents将JSON字符串加载到JSON_decode函数时,我必须先除去BOM字符,即utf-8ef bb bf,然后才能正常工作。
    比较两个字符串的长度——硬编码的和传入的变量——如果它们不相同,那么这些字符可能是罪魁祸首。

        9
  •  0
  •   jQuery    10 年前

    您可以在提交我端解决的问题之前设置数据库字符集:

    $sql = $mysqli->set_charset("utf8");
    
    推荐文章