Jump to content

Problem parsing valid JSON in PHP


Vampyress86

Recommended Posts

Hey there,

 

I am using prototype to send JSON information to a PHP file which will then use the data to update a database, the AJAX presently looks as follows:

 

function saveprofile()
{
var email = document.getElementById("email").value;

var profileDetails = 
{
"email":email,
"titleitalic":titleitalic,
"profileDetails":titlefontcolor,
"titlefontcolor":titleunderline,
"titleoffsettop":titleoffsettop,
"titleoffsetleft":titleoffsetleft,
"aboutbackgroundcolour":aboutbackgroundcolour,
"aboutunderline":aboutunderline,
"aboutfontfamily":aboutfontfamily,
"aboutbold":aboutbold,
"aboutitalic":aboutitalic,
"aboutfontcolor":aboutfontcolor,
"aboutoffsettop":aboutoffsettop,
"aboutoffsetleft":aboutoffsetleft,
"statusoffsetleft":statusoffsetleft,
"statusinfooffsetright":statusinfooffsetright,
"pictureoffsetleft":pictureoffsetleft,
"profilebold":profilebold,
"profileunderline":profileunderline,
"profileitalic":profileitalic,
"profilefontcolor":profilefontcolor,
"profilefontfamily":profilefontfamily,
"profilebackgroundcolour":profilebackgroundcolour
}

var jsonstring = JSON.stringify(profileDetails);
alert(jsonstring);

   var request = new Ajax.Request
        ('scripts/updateprofile.php',
            { method: 'POST',
              parameters: jsonstring,
              onComplete: responseReturned }
        );
}	

 

Which outputs the following JSON which has been tested on http://www.jsonlint.com/ as valid:

 

{
    "email": "l-wilson-1986@hotmail.co.uk",
    "titleitalic": "normal",
    "profileDetails": "#d0deea",
    "titlefontcolor": "none",
    "titleoffsettop": 100,
    "titleoffsetleft": 100,
    "aboutbackgroundcolour": "transparent",
    "aboutunderline": "none",
    "aboutfontfamily": "Arial",
    "aboutbold": "normal",
    "aboutitalic": "normal",
    "aboutfontcolor": "#d0deea",
    "aboutoffsettop": 325,
    "aboutoffsetleft": 100,
    "statusoffsetleft": 450,
    "statusinfooffsetright": 250,
    "pictureoffsetleft": 100,
    "profilebold": "normal",
    "profileunderline": "none",
    "profileitalic": "normal",
    "profilefontcolor": "#fff",
    "profilefontfamily": "Arial",
    "profilebackgroundcolour": "#2c3c49"
}

 

The PHP file which receives the JSON looks as follows:

 

<?php

$myjson = $_POST['jsonstring'];
$jsonchars = json_decode(($myjson), 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;

// do something with the data

?>

 

The problem is the PHP constantly returns the following error: Last error : Syntax error and any attempts to access the JSON data returns NULL values. I have looked at several online examples and cannot see where I might be going wrong, and although users have had problems with syntax errors it normally relates to incorrect JSON.

 

Any advice on this topic would be greatly appeciated :)

 

Leanne

Link to comment
Share on other sites

I tested with:

<?php
$json_str = '{
    "email": "l-wilson-1986@hotmail.co.uk",
    "titleitalic": "normal",
    "profileDetails": "#d0deea",
    "titlefontcolor": "none",
    "titleoffsettop": 100,
    "titleoffsetleft": 100,
    "aboutbackgroundcolour": "transparent",
    "aboutunderline": "none",
    "aboutfontfamily": "Arial",
    "aboutbold": "normal",
    "aboutitalic": "normal",
    "aboutfontcolor": "#d0deea",
    "aboutoffsettop": 325,
    "aboutoffsetleft": 100,
    "statusoffsetleft": 450,
    "statusinfooffsetright": 250,
    "pictureoffsetleft": 100,
    "profilebold": "normal",
    "profileunderline": "none",
    "profileitalic": "normal",
    "profilefontcolor": "#fff",
    "profilefontfamily": "Arial",
    "profilebackgroundcolour": "#2c3c49"
}';
$x = json_decode(($json_str),true);
echo '<pre>' . print_r($x,true) . '</pre>';
$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;
?>

 

And it works fine.  There has to be something wrong with the incoming string. If you have magic quotes turned on there are probably backslashes escaping the quotes in the string. Use stripslashes on it before passing it to json_decode

 

<?php
$myjson = stripslashes($_POST['jsonstring']);
?>

 

Ken

Link to comment
Share on other sites

Hey Ken,

 

Thankyou for your reply and my late response. It actually turns out the problem came from the #'s in the colour code, all this time and all I had to do was use var_dump($myjson); to see where my json string halted haha.

 

Thankyou for your time and response :)

 

Leanne

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.