0

No idea why this won't convert, I would assume it may have something to do with the string, but I get np output.

$string = '[{title : "Comp 1 Product",columns : ["Our Vehicle","Features","Their Vehicle"], items : [["dcq","adv","asdvasdv"],["sdv","sdv","sdv"]]},{title : "qwefqw",columns : ["Section 1","Features","Section 2"],items : [["qqwec","qwe","qwegqwev"]]}]';

print_r(json_decode($string), true);

Any help would be appreciated!

7
  • 2
    It's not valid JSON. In JSON, property names need to be in double quotes. Commented Dec 27, 2016 at 22:47
  • 1
    Where did that string come from? You need to fix the source so that it creates the JSON properly. It should use a JSON library instead of constructing it by hand. Commented Dec 27, 2016 at 22:49
  • 1
    I have posted the way to debug such problems and identify what the problem is. Please correct at the source. Commented Dec 27, 2016 at 22:50
  • 1
    Also, give the true to the left of bracket. i.e., print_r(json_decode($string, true)); Commented Dec 27, 2016 at 23:01
  • 1
    @GregAlexander JS and JSON are two different animals from the same mother... :D Commented Dec 28, 2016 at 3:36

2 Answers 2

6

If you see:

<?php
    header("Content-type: text/plain");
    $string = '[{title : "Comp 1 Product",columns : ["Our Vehicle","Features","Their Vehicle"], items : [["dcq","adv","asdvasdv"],["sdv","sdv","sdv"]]},{title : "qwefqw",columns : ["Section 1","Features","Section 2"],items : [["qqwec","qwe","qwegqwev"]]}]';
    print_r(json_decode($string), true);
    print_r(json_last_error());
?>

The above code returns a 4, which means JSON_ERROR_SYNTAX, which is a syntax error with JSON. When checked with JSON Lint, your JSON throws:

prevoew

You need to correct it to look like:

[{
    "title": "Comp 1 Product",
    "columns": ["Our Vehicle", "Features", "Their Vehicle"],
    "items": [
        ["dcq", "adv", "asdvasdv"],
        ["sdv", "sdv", "sdv"]
    ]
}, {
    "title": "qwefqw",
    "columns": ["Section 1", "Features", "Section 2"],
    "items": [
        ["qqwec", "qwe", "qwegqwev"]
    ]
}]

What you have now is a JavaScript Object and not a valid JSON!

Sign up to request clarification or add additional context in comments.

2 Comments

I was about to post this :) +1
@Farkie Thanks mate... :)
0

Apart from invalid json, print_r(json_decode($string), true); prints nothing, but returns the value. To get the result printed to the output, you need to either:

print_r(json_decode($string));

or

echo print_r(json_decode($string), true);

The former is better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.