-1

I have my json value coming from java, as

var obj = "[{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"}] ".

I want to parse using jquery as $.parseJSON(obj);

I am getting error as undefined. I want to get the id value.

I included the jquery-ui.js and jquery.js.

I checked in the jsonlint and get it as valid json.

3
  • how you are accessing the id for it... this is an array of objects can you past how you tried to access the id. Commented Oct 30, 2012 at 6:37
  • 2
    what you tried Please post that too?
    – Jericho
    Commented Oct 30, 2012 at 6:37
  • Please show your JS code. If you're using one of jQuery's Ajax functions to retrieve this object (as implied by the "jquery-ajax" tag on your question) then you shouldn't need to use $.parseJSON() because jQuery will parse the string for you and pass the resulting object (in this case an array) to the callback function you provide.
    – nnnnnn
    Commented Oct 30, 2012 at 6:47

2 Answers 2

2

JavaScript strings containing " characters must be either delimited by ' character or have the " escaped as \".

With your current code you should have errors trying to parse the JS that attempts to instantiate obj before it complains that obj is undefined.

0

please add the escape params for the string first like the below.

var obj = "[{\"id\":\"58\",\"str\":\"sd <p/> Comment By Smith John on 2012-10-30\"},{\"id\":\"58\",\"str\":\"sd <p/> Comment By Smith John on 2012-10-30\"}]"

and then take the parsed object to a variable and access the id in the below manner..

var tmp = $.parseJSON(obj); tmp[1].id;

hope you got the answer.

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.