Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AngularJS - how to query JSON and return a value

Posted on 2015-01-08
4
Medium Priority
?
4,311 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 200 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 200 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 1600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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)
Suggested Courses

782 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