Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jquery search in JSON file

Posted on 2010-09-10
3
Medium Priority
?
630 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 2000 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…

636 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