Solved

jquery search in JSON file

Posted on 2010-09-10
3
607 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
ID: 33647968
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
ID: 33647987
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
ID: 33659428
You're welcome! Thanks for the points!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular - filter - one works but other doesn't 13 32
gif animation 6 35
jquery easyautocomplete for mutiple text boxes 9 55
Angular - "nest" service calls? 18 24
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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)

920 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