Solved

Jquery parseJSON - json

Posted on 2010-11-16
3
924 Views
Last Modified: 2012-05-10
Hi - I am trying to parse a json object

I have 3 sets of data they are as follows

"from": {"name": "Bill Smith", "category": "Technology","id": "19292868552"}

"to": {"name": "Joe Hunt", "category": "Technology","id": "11204705797"}

"comments": {"name": "Sisi", "message": "Hi from Sisi", "id": "11204705797"},{"name": "Sara", "message": "Hi from Sarah", "id": "11204705797"}

I would like to create a json string/object and then have jquery parse the information

I can do it with 2 sets of data see code below...

Now i need to add the third set and parse/loop though though it!

Thanks in advance








------2 sets of data working!!!!-----------------------------------------------------

var obj = jQuery.parseJSON('{"from": {"name": "Bill Smith", "category": "Technology","id": "19292868552"}, "to": {"name": "Joe Hunt", "category": "Technology","id": "11204705797"}}');

alert(obj.from.name);

alert(obj.to.name);

-------------------------------------------------------------------------------------



'3 set of data I NEED HELP!!--------------------------------------------------------------



var obj = jQuery.parseJSON('{"from": {"name": "Bill Smith", "category": "Technology","id": "19292868552"}, "to": {"name": "Joe Hunt", "category": "Technology","id": "11204705797"}, "comments": [{"name": "Sisi", "message": "Hi from Sisi", "id": "11204705797"},{"name": "Sara", "message": "Hi from Sarah", "id": "11204705797"}]  }');



alert(obj.from.name);

alert(obj.to.name);



***THIS IS WHERE I GET LOST!! I need it to loop in comments and give an output (for each)

alert(obj.comments.message); - NOT WORKING



The output I hope for would be...

Message1 "Bill Smith"

Message2 "Joe Hunt"

Message3 "Hi from Sisi"

Message4 "Hi from Sarah"

Open in new window

0
Comment
Question by:doctor069
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Use :

obj.comments[0].message
and
obj.comments[1].message

because comments is an array of object

Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script language="javascript">
	$(document).ready(function() {

		var obj = jQuery.parseJSON('{"from": {"name": "Bill Smith", "category": "Technology","id": "19292868552"}, "to": {"name": "Joe Hunt", "category": "Technology","id": "11204705797"}}');
		alert(obj.from.name);
		alert(obj.to.name);

		var obj = jQuery.parseJSON('{"from": {"name": "Bill Smith", "category": "Technology","id": "19292868552"}, "to": {"name": "Joe Hunt", "category": "Technology","id": "11204705797"}, "comments": [{"name": "Sisi", "message": "Hi from Sisi", "id": "11204705797"},{"name": "Sara", "message": "Hi from Sarah", "id": "11204705797"}]  }');		
		//Message1 "Bill Smith"
		alert(obj.from.name);
		//Message2 "Joe Hunt"
		alert(obj.to.name);		
		//Message3 "Hi from Sisi"
		alert(obj.comments[0].message);
		//Message4 "Hi from Sarah"
		alert(obj.comments[1].message);
	});
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:doctor069
Comment Utility
Perfect! Thank you very much
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
You're welcome! Thanks for the points!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now