Getting fields from JSON using Angularjs

2019-09-17 11:32发布

问题:

This is a very simple question but I'm writing a webpage that's supposed to display the user's information. There will be fields of name, id, role, etc. I want to write setter and getter methods for the JSON that I receive from the database. For example, if I get a JSON and I want to get and display the name field, how would I write this getter method in angularJS after getting the JSON from a $resource service?

This is an example of how the JSON would look:

[{"name": "Jason"
  "date of birth": "february 23, 2985"
  ....
 }]

Again, I just want to know how to write a getter method if I wanted to get the name field.

回答1:

Should be just like javascript.

var result = [{"name": "Jason"
  "date of birth": "february 23, 2985"
  ....
 }];

var firstResultsName = result[0].name;

But i noticed a problem with the result that you gave. You need a comma after each property.

[{"name": "Jason", <<-- this comma is necessary to work correctly.
"date of birth": "february 23, 2985",  <<-- and this one.

make sure you have commas for all but the last property.