Solved

AngularJS - how to query JSON and return a value

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

734 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