[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Jquery parseJSON - json

Posted on 2010-11-16
3
Medium Priority
?
986 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 34151842
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
ID: 34151888
Perfect! Thank you very much
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34151891
You're welcome! Thanks for the points!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

650 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