I've an issue with my JSON. It works returns correctly in PHP 5.3 (so I can't use json_last_error()), and i开发者_开发问答t returns successfully when I copy string explicitly into json_decode (json_decode('{...}'). It only returns null in when I pass the result as a variable and I'm using php 5.2, which is what I need it for.
The output comes from JSON logging in PHPUnit:
[
{
"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": ""
}
]
EDIT: These are the paths, I've been exploring - maybe you are a better explorer.. Three possible paths that could help:
- What is different about json_decode() in php 5.2 then 5.3? what did they change?
- Someone else using JSON from PHPUnit, and how they parse it.
- What changes when you have it in a variable vs. printing it to screen and copying it into json_decode()
Any help would be greatly(!) appreciated.
Thanks! Matt
What a HORRENDOUS debug session.. well there's good news.. I figured it out..
I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle "
, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:
$json = str_replace('"', '"', $json);
Now I thought they were the same.. maybe someone can enlighten me..
Yesterday I spent 2 hours on checking and fixing that error finally I found that in JSON string that I wanted to decode were '\' slashes. So the logical thing to do is to use stripslashes function or something similiar to different PL.
Of course the best way is sill to print this var out and see what it becomes after json_decode, if it is null you can also use json_last_error() function to determine the error it will return integer but here are those int described:
0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8
In my case I got output of json_last_error() as number 4 so it is JSON_ERROR_SYNTAX. Then I went and take a look into the string it self which I wanted to convert and it had in last line:
'\'title\' error ...'
After that is really just an easy fix.
$json = json_decode(stripslashes($response));
if (json_last_error() == 0) { // you've got an object in $json}
When I use:
phpunit --log-json file.json <test_file>
(using PHPUnit 3.4.13), The file that it creates does not appear to contain valid JSON,
the json file contains "json" which looks something like:
{...}{...}{...}{...}
Instead of what I would expect to see:
[{...},{...},{...},{...}]
Not sure if the is the same problem that you're seeing, your sample JSON output in the question appears to be more valid that what I'm seeing.
Once adding the missing commas and brackets, it can be parsed with json_decode() on PHP 5.2.10 or PHP 5.3.2.
Since PHP 7.3, the json_decode function will accept a new JSON_THROW_ON_ERROR option that will let json_decode throw an exception instead of returning null on error.
Example:
try {
json_decode("{", false, 512, JSON_THROW_ON_ERROR);
}
catch (\JsonException $exception) {
echo $exception->getMessage(); // displays "Syntax error"
}
You should use this
$jsonstring = stripslashes(str_replace('\"', '"', $jsonstring));
I tested this with PHP 5.3
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);
try to set the error reporting in ALL, the json_decode() should give you a notice in the offset where the conversion fails.
Useful post from Sebastian about the json output format
https://github.com/sebastianbergmann/phpunit/issues/142
Using Keith's suggestion will allow the data to be parsed correctly
I notice this behavior with PHP version 5.14.12, and it may be for others versions as well.
When using file_get_contents to load a JSON string into json_decode function, I had to strip out the BOM characters i.e. for UTF-8 EF BB BF before it would work properly.
Compare the lengths of your two strings --the hard coded versus the passed in variable-- if they are not the same, these characters may be the culprit.
You can set the database charset before submitting - solved issues on my end:
$sql = $mysqli->set_charset("utf8");
精彩评论