0

I'm trying to put my data into Json format. The code seems to work fine, I get a Json format, but the data is not Json valid. This is how the json comes out:

({
"sample": [
    {
        "0": "12",
        "image_id": "12",
        "1": "background.JPG",
        "path": "background.JPG",
        "2": "background.JPG",
        "name": "background.JPG",
        "3": "image\/jpeg",
        "type": "image\/jpeg",
        "4": "51600",
        "size": "51600",
        "5": "4",
        "likes": "4",
        "6": "zwitserland",
        "onderwerp": "zwitserland",
        "7": "Landscape from cableway",
        "beschrijving": "Landscape from cableway"
    },
    {
        "0": "13",
        "image_id": "13",
        "1": "IMG_1052.JPG",
        "path": "IMG_1052.JPG",
        "2": "IMG_1052.JPG",
        "name": "IMG_1052.JPG",
        "3": "image\/jpeg",
        "type": "image\/jpeg",
        "4": "45434",
        "size": "45434",
        "5": "28",
        "likes": "28",
        "6": "belgium",
        "onderwerp": "belgium",
        "7": "Highway in Belgium",
        "beschrijving": "Highway in Belgium"
    }, ETC ETC

This is my php code. When I put my code into a Json editor I get the following error:

Parse error on line 1: ({ "sample": [ ^ Expecting '{', '['

$arr = array();
$rs = mysql_query("SELECT * FROM images");

while($obj = mysql_fetch_array($rs)) {
$arr[] = $obj;

}
$json = '{"sample":'.json_encode($arr).'}';
echo $_GET['jsoncallback'] . '(' . $json . ')';
1
  • It IS proper JSON until you wrapped it in parenthesis Commented Dec 5, 2013 at 13:53

1 Answer 1

3

You shouldn't try manually building your own JSON, for reasons exactly like this.

Use this and this alone.

$json = json_encode(array("sample" => $arr));

Or

$json = json_encode(array("sample" => array($_GET["jsoncallback"] => $arr)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.