Solved

Jquery parseJSON - json

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

707 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