?
Solved

Jquery parseJSON - json

Posted on 2010-11-16
3
Medium Priority
?
977 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

752 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