1

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?

1
  • What you have is already an object. No need to parse it. Like @mimipc said, you need quotes to make it a string. Commented Feb 4, 2013 at 8:46

3 Answers 3

2
var object = {"nick":"person1", "text":"hello "};
console.log(object.nick);
Sign up to request clarification or add additional context in comments.

2 Comments

for some reason this does not work, ive used console.log(text) and this is what i get: {"nick":"person1", "text":"hello "} but when i do console.log(text.nick) i get "unidentified"
are u using like this '{"nick":"person1", "text":"hello "}', if so remove quotes.
1

Have you put quotes around your var value ?

var text = '{"nick":"person1", "text":"hello "}';

Comments

0

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

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.