Solved

jquery search in JSON file

Posted on 2010-09-10
3
604 Views
Last Modified: 2012-05-10
I was wondering using jquery i need a example of how to search thru a JSON file.  So lets say i look for Item1 it will return the fields bar and items.
{

	"items": [



	{

	  "foo": "Item0",

	  "bar": "HEllo",

	  "items": {"www.google.com" : "lgo_google.gif"},

	},

	

	{

	  "foo": "Item1",

	  "bar": "Good Bye",

      "items": {"www.yahoo.com" : "lgo_yahoo.gif"},

	},

	

	{

	  "foo": "Item2",

	  "bar": "What up",

	  "items": {"www.msn.com" : "lgo_msn.gif"},

	}



			

	]

}

Open in new window

0
Comment
Question by:stargateatlantis
  • 3
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
You may use :


function getObject(searchTerm, json) {
		var items = json.items;
		for(i=0;i<json.items.length;i++) {
			for(var j in items[i]) {
				if(items[i][j] == searchTerm) return items[i];
			}
		}
	}

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
Comment Utility
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.2.min.js"></script>
<script language="javascript">

	var myJSON = {"items":[{"foo":"Item0","bar":"HEllo","items":{"www.google.com":"lgo_google.gif"},},{"foo":"Item1","bar":"Good Bye","items":{"www.yahoo.com":"lgo_yahoo.gif"},},{"foo":"Item2","bar":"What up","items":{"www.msn.com":"lgo_msn.gif"},}]};

	$(document).ready(function() {
		alert( getObject("Item1", myJSON).bar );
		alert( getObject("Item1", myJSON).items );
	});

	function getObject(searchTerm, json) {
		var items = json.items;
		for(i=0;i<json.items.length;i++) {
			for(var j in items[i]) {
				if(items[i][j] == searchTerm) return items[i];
			}
		}
	}
</script>
</head>

<body>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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

14 Experts available now in Live!

Get 1:1 Help Now