Solved

AngularJS - how to query JSON and return a value

Posted on 2015-01-08
4
3,224 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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

777 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