最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery.parseJSON() Invalid Character error - Stack Overflow

programmeradmin1浏览0评论

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);

            }

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);

            }
Share Improve this question asked Nov 16, 2013 at 17:34 Ankit AgrawalAnkit Agrawal 3252 gold badges5 silver badges16 bronze badges 1
  • The value you pass to $.parseJSON is already an object, not a string containing JSON. Just remove the function call. "[...] when parsing string to JSon [...]" I think you are misunderstanding something here. In the context of JavaScript, JSON can only be contained in a string. JSON is a textual representation of data. You can convert arrays and object to JSON and convert a string containing JSON to arrays and object. – Felix Kling Commented Nov 16, 2013 at 17:38
Add a ment  | 

4 Answers 4

Reset to default 4

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...
}

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"}');

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);
};

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.

发布评论

评论列表(0)

  1. 暂无评论