Getting fields from JSON using Angularjs

血红的双手。 提交于 2019-12-12 01:44:44

问题


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.



来源:https://stackoverflow.com/questions/24496874/getting-fields-from-json-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!