Solved

AngularJS - how to query JSON and return a value

Posted on 2015-01-08
4
2,933 Views
Last Modified: 2015-01-09
What is the proper way to query json and return a value?  Can you please provide an example?

How can I query Employees - find firstname = "Peter" and return "Jones"

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}
0
Comment
Question by:JElster
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:Imran Javed Zia
Imran Javed Zia earned 50 total points
ID: 40539750
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 40539753
And from jQuery you can use following:

http://api.jquery.com/jQuery.grep/
0
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 50 total points
ID: 40539846
Hi,
sorry I am currently very limited in time, but the following two tutorials should shade in some lights:
http://blog.hfarazm.com/filters-in-angularjs/
http://blog.hfarazm.com/data-binding-angularjs/
HTH
Rainer
0
 
LVL 22

Accepted Solution

by:
ambience earned 400 total points
ID: 40539913
Getting java object from JSON string is as simple as

var employees = angular.fromJson(jsonStr);

As for finding an item in the returned object, and doing it using angularJS,

    angular.forEach(employees, function(value) {
       if(value.firstName == name) this.push(value);
    }, employees);

However, doing it the angular way is to write a filter for that

This should help: http://jsbin.com/cawesiqeqi/1/edit?html,js,output
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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…

758 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

24 Experts available now in Live!

Get 1:1 Help Now