Reputation: 9151
I have the following json string:
{"nick":"person1", "text":"hello "}
I want to convert it into a object to access each variable.
I've tried the following:
var obj = $.parseJSON(text);
alert(obj.nick);
Where text is the json string.
This gives and error "Illegal character".
However, if i copy and paste the string itself and insert it into the parseJSON function it works..
Any ideas what could be wrong?
Upvotes: 1
Views: 94
Reputation: 74738
try this:
var text = '{"nick":"person1", "text":"hello "}';
var obj = $.parseJSON(text);
alert(obj.nick);
this is simple json:
{"nick":"person1", "text":"hello "}
to make it string wrap it with single quotes:
'{"nick":"person1", "text":"hello "}'
Upvotes: 0
Reputation: 1374
Have you put quotes around your var value ?
var text = '{"nick":"person1", "text":"hello "}';
Upvotes: 1
Reputation: 4347
var object = {"nick":"person1", "text":"hello "};
console.log(object.nick);
Upvotes: 2