Solved

jquery search in JSON file

Posted on 2010-09-10
3
620 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
[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
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
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…

690 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