phpjavascriptjsonvalidationjsonlint

json error: parsererror


I am developing a balloon notification for my social network. I came across this error when there's 2 or more notifications. I checked my JSON response on jsonlint.com, and I got error at line 6.

JSON response:

    {
    "nid": "1101",
    "img": "<img src=\".\/images\/icons\/he_wall_post_icon.png\">",
    "notifier": "Sarah O&#039;conner",
    "url": " has commened on your <a href=\"wall_action.php?id=1463\">post<\/a>"
}{
    "nid": "1100",
    "img": "<img src=\".\/images\/icons\/he_wall_post_icon.png\">",
    "notifier": "Sarah O&#039;conner",
    "url": " likes your <a href=\"wall_action.php?id=1463\">post<\/a>"
}

here's my PHP part:

$ret_arr = array('nid' => $nid2,'img' => $img, 'notifier' => $notifier, 'url' => $url);

echo json_encode($ret_arr);

here's my JS part:

    function noob()
{
    jQuery.ajax({
        url: 'notifications.php?n=1',
        dataType: 'json',
        success: function(data){
            alert('Success!');
        },
        error: function(requeset, textStatus, errorThrown){
            alert('error:'+textStatus);
        }
    });
}

How can i get that done!

Thanks guys.


Solution

  • Your JSON is missing the list brackets and the comma between objects.

    It should look like:

    [
      {
        "nid": "1101",
        "img": "<img src=\".\/images\/icons\/he_wall_post_icon.png\">",
        "notifier": "Sarah O&#039;conner",
        "url": " has commened on your <a href=\"wall_action.php?id=1463\">post<\/a>"
      },
      {
        "nid": "1100",
        "img": "<img src=\".\/images\/icons\/he_wall_post_icon.png\">",
        "notifier": "Sarah O&#039;conner",
        "url": " likes your <a href=\"wall_action.php?id=1463\">post<\/a>"
      }
    ]