Solved

jquery search in JSON file

Posted on 2010-09-10
3
617 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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
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 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)

738 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