0

I'm having a problem. I have a json code as below. I want to parse them but get an error: SyntaxError: JSON.parse: unexpected character I don't know where is the error. Would anyone help?

My js code:

  function retreive() {
  var userInfo = new Array();
  userInfo[0] = $("#contactId").val();
  userInfo[1] = $("#pw").val();
  var cId = $.ajax({ 
    url: "server.php", 
    type: "POST", 
data: {phpData : userInfo}, 
    datatype: "json",
success:function(msg) {
    responseJson = JSON.parse(msg.responseText);
var outputHtml = "";
    for (var i=0; i<responseJSON.user.mary.length; i++) {
outputHtml += responseJSON.user.mary[i].sender[i].sendDate + 
", " + responseJSON.user.mary[i].sender[i].time + 
", " + responseJSON.user.mary[i].sender[i].timezone + 
", " + responseJSON.user.mary[i].sender[i].message + "<br/>"}
divMessage = document.getElementById("message");
    divMessage.innerHTML = outputHtml;
}
});   

}

my php code:

    $data = '{
  "user" : 
    [
      {
    "mary" :
      [
        {
          "sender1" :
        [
              {
                "sendDate"     : "2012-01-13",
                "time"     : "15:00:21",
                "timezone" : "Asia/Hong_Kong",
                "message"  : "hi"
          },
          {
                "sendDate"     : "2012-01-18",
                "time"     : "16:00:01",
                "timezone" : "Asia/Hong_Kong",
            "message"  : "how are you"
              },
              {
                "sendDate"     : "2012-01-21",
                "time"     : "14:31:42",
                "timezone" : "Asia/Hong_Kong",
            "message"  : "good"
          }  
        ],
          "sender2" :
        [
          {
                "sendDate"     : "2012-01-14",
                "time"     : "09:01:25",
                "timezone" : "Asia/Hong_Kong",
                "message"  : "good morning"
          },
              {
                "sendDate"     : "2012-01-14",
                "time"     : "09:03:41",
            "timezone" : "Asia/Hong_Kong",
                "message"  : "where are you"
          },
          {
                "sendDate"     : "2012-01-14",
                "time"     : "09:05:42",
                "timezone" : "Asia/Hong_Kong",
                "message"  : "me too"
          }
            ],
        }  
      ],    
    "peter" :
      [
        {
          "sender1" :
            [         
              {
                "sendDate"     : "2012-01-13",
                "time"     : "10:44:28",
            "timezone" : "Asia/Hong_Kong",
                "message"  : "hey man"
          },
          {
                "sendDate"     : "2012-01-13",
                "time"     : "10:46:11",
                "timezone" : "Asia/Hong_Kong",
                "message"  : "what are you doing"
              },
              {
                "sendDate"     : "2012-01-13",
                "time"     : "10:48:33",
                "timezone" : "Asia/Hong_Kong",
                "message"  : "nice"
          }
        ],
          "sender3" :
            [
              {
                "sendDate"     : "2012-01-18",
                "time"     : "14:23:58",
            "timezone" : "Asia/Hong_Kong",
                "message"  : "Had you send the file to me"
              },
          {
                "sendDate"     : "2012-01-18",
                "time"     : "15:01:39",
            "timezone" : "Asia/Hong_Kong",
                "message"  : "i have not receive yet"
          },
              {
                "sendDate"     : "2012-01-19",
                "time"     : "09:08:32",
                "timezone" : "Asia/Hong_Kong",
                "message"  : "received"
          },
        ],  
        }
      ],
      }
    ],
}';
echo $data;
8
  • 2
    @Art, also, please refrain from screaming at us.. :) Commented Aug 1, 2012 at 12:43
  • 1
    Trailing commas are not always supported... Commented Aug 1, 2012 at 12:44
  • 2
    json.parser.online.fr This might help. Copy-paste parts of your JSON to see where is the excess/missing whatever character :) Commented Aug 1, 2012 at 12:45
  • 3
    @Vishal - trailing commas always invalidate JSON. Some browsers allow them in JS objects (not the same thing as JSON) but IE will rightly throw an error. Commented Aug 1, 2012 at 12:46
  • yip, remove all you're final commas and it validates. Commented Aug 1, 2012 at 12:46

2 Answers 2

8

Well, like the error says, your JSON is invalid.

http://jsonlint.com/

Constructing JSON manually is a really bad idea. It's far harder to construct, and you're prone to parse errors. Instead, build your data programmatically as an array or object then use json_encode().

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

Comments

2

JSON Lint is your friend, use it to find potential errors in your json.

Parse error on line 45:
...                   }            ],   
----------------------^
Expecting 'STRING'

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.