TJR
TJR

Reputation: 6577

json decode in php problems

I have the fallowing string in 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. 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 ...

Upvotes: 0

Views: 145

Answers (3)

JithinVG
JithinVG

Reputation: 3

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

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

?>

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

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.

["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.

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"

Upvotes: 3

GaryDevenay
GaryDevenay

Reputation: 2415

It doesn't work because it is not valid JSON. You can find the correct JSON formatting here: http://www.w3schools.com/json/default.asp

Upvotes: 0

Related Questions