简体   繁体   English

JSON在PHP解码问题

[英]json decode in php problems

I have the fallowing string in php: 我在php中有休闲字符串:

    $code = "[[],["Mon","01","  1.7","  8","3"," 96","33","
29.01.2013"],["Tue","01","  0.3"," 24","2","100","16","
30.01.2013"],["Wed","01","  5.4"," 28","2"," 98","5","
31.01.2013"],["Thu","01","  8.7"," 22","3"," 92","23","
01.02.2013"],["Fri","01","  5.1"," 43","3"," 91","22","
02.02.2013"],["Sat","01","  2.8"," 18","2"," 90","22","
03.02.2013"],["Sun","01","  2.1"," 31","6"," 93","34","
04.02.2013"]]";

Now i try to decode this string with json_decode. 现在,我尝试使用json_decode解码此字符串。 But the result ist this one: 但是结果却是这样的:

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

The code to output is this: 输出的代码是这样的:

$code = json_decode($code);
print_r($code);

Why this dont work ? 为什么这不起作用? THis is the first time i have problems with json_decode ... 这是我第一次遇到json_decode问题...

Assuming that the contents of $code are all in a string (and not a php array like the syntax is now), the error is the fact that you have newlines inside of the strings. 假设$code的内容全部在一个字符串中(而不是像现在这样的php数组),则错误是因为字符串中包含换行符。

["Mon","01","  1.7","  8","3"," 96","33","

Notice how there is an open quote at the end of the line .. that makes for invalid JSON. 请注意,在行尾..处如何有一个引号引起无效的JSON。

If you get rid of all of the newlines, it actually does work. 如果您摆脱了所有换行符,它实际上可以工作。 Here's my proof: 这是我的证明:

array(8) {
  [0]=>
  array(0) {
  }
  [1]=>
  array(8) {
    [0]=>
    string(3) "Mon"
    [1]=>
    string(2) "01"
    [2]=>
    string(5) "  1.7"
    [3]=>
    string(3) "  8"

It doesn't work because it is not valid JSON. 它无效,因为它不是有效的JSON。 You can find the correct JSON formatting here: http://www.w3schools.com/json/default.asp 您可以在此处找到正确的JSON格式: http//www.w3schools.com/json/default.asp

This does not seems to be like a valid json string.You would get the required result if you try it with a valid json string like 这似乎不像是一个有效的json字符串。如果使用有效的json字符串(例如)尝试将获得所需的结果

<?php
$code=... //a valid json string
$result=json_decode($code,true); // now $result will contain an associative array
print_r($result);

?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM