1

I want to parse this json using jquery and each with a loop. I have only seen json examples where it is wrapped in array with a title such as results. How do I parse json like below...?

$.each(json.results, function() {
    console.log(this['title']);
});
[{"title":"About Us","url_title":"about_us","entry_id":"155","channel_id":"2","author_id":"1","status":"open","entry_date":"1287508538","edit_date":"20101209122240","expiration_date":"0","about_body":"We are the greatest.","about_image":"","about_staff_title":"Joe","about_extended":"More about us.","color":"d12626"},{"title":"Contact Us","url_title":"contact_us","entry_id":"223","channel_id":"2","author_id":"1","status":"open","entry_date":"1291918929","edit_date":"20101209122310","expiration_date":"0","about_body":"Email us.","about_image":"","about_staff_title":"Bob","about_extended":"","color":"080480"}]
2
  • What is your expected outcome? Commented Dec 18, 2011 at 5:18
  • 2
    Like this? jsfiddle.net/TuhHt Commented Dec 18, 2011 at 5:21

2 Answers 2

3
$.each(json, function() {     
    console.log(this.title);
});
Sign up to request clarification or add additional context in comments.

3 Comments

@digitalbart - This is essentially the same answer as my comment.
@JaredFarrish Do you want to post an answer and I will accept it
@digitalbart - No, it's ok. We posted it at the same time.
1

Shouldn't it just be $.each(json, function() ... ) ? Your JSON is an array, no wrapper, so just pass it directly to $.each().

3 Comments

I am not sure what you by passing it directly to $.each()? Can you elaborate
What both @Sudhir and @Jared show - if your JSON is an array, and you load it into a variable called json, just pass json as the first argument to $.each, rather than passing json.results.
Thanks that makes sense I over thought it.

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.