Solved

Jquery parseJSON - json

Posted on 2010-11-16
3
955 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
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

839 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