Ankit Agrawal
Ankit Agrawal

Reputation: 325

jQuery.parseJSON() Invalid Character error

I am getting "Invalid Character Error" when parsing string to JSon but don't know the reason.. please help me out, Here is My code

var data = $.parseJSON({ "senderEmail": "[email protected]", "RecieverEmail": "alll", "Message": "j", "DateAndTime": "2013"});
            for (var i = 0; i < data.length; i++) {
                console.log("--------------------");

                console.log(data[i].SenderEmail);
                console.log(data[i].RecieverEmail);
                console.log(data[i].DateTime);
                console.log(data[i].Message);

            }

Upvotes: 3

Views: 22729

Answers (4)

Paulo Freitas
Paulo Freitas

Reputation: 13649

First: you already have a JSON object here, jQuery.parseJSON() takes a string as input.

Second: to access data[i] properties you'll also have to pass a list of objects.

So:

var data = $.parseJSON('[{"senderEmail": "[email protected]", "ReceiverEmail": "alll", "Message": "j", "DateAndTime": "2013"}]');

for (var i = 0; i < data.length; i++) {
    // Now you can access data[i] properties as you want...
}

Upvotes: 4

The Alpha
The Alpha

Reputation: 146269

This is already an object (without quotes), and not a json string, so nothing to parse

{
    "senderEmail": "[email protected]",
    "RecieverEmail": "alll",
    "Message": "j",
    "DateAndTime": "2013"
}

A json string should be something like this

'{ "senderEmail": "[email protected]", "RecieverEmail": "alll", "Message": "j", "DateAndTime": "2013"}'

Notice the quotes '{...}'. This is an example of looping an object and this example shows how to loop an object after parsing it from a json string, see the difference.

So, if your data is already an object then you don't need to use $.parseJSON because it just convert the json string to a JavaScript object but if this is a json string '{...}' then parse it (convert the json string to an object) then you loop it. json means JavaScript Object Notation and a json string is just a JavaScript object inside a string ('{...}' and not a real Object) and to make it a real JavaScript Object we need to parse it first, unless, we can't use it as an Object.

Read more on json.org.

Upvotes: 1

Sardor I.
Sardor I.

Reputation: 426

First of all you trying to parse JSON itself, it is not string

var data = $.parseJSON('[{ "senderEmail": "[email protected]", "RecieverEmail": "alll",   "Message": "j", "DateAndTime": "2013"}]');
  for (var i = 0; i < data.length; i++) {
  console.log("--------------------");
  console.log(data[i].senderEmail);
  console.log(data[i].RecieverEmail);
  console.log(data[i].Message);
  console.log(data[i].DateAndTime);
};

Upvotes: 1

JAAulde
JAAulde

Reputation: 19560

JSON is a string which is a serialized piece of data using a subset of the JS literal syntax.

In your example you are not passing a string to parseJSON, you're passing an object literal.

You want:

var data = $.parseJSON('{"senderEmail": "[email protected]", "RecieverEmail": "alll", "Message": "j", "DateAndTime": "2013"}');

Upvotes: 3

Related Questions