来自烧瓶的JSON

时间:2015-12-21 11:29:39

标签: angularjs json restangular

我正在尝试从一个使用angularjs和restangular从我生成的url中获取数据。 我有这个json数据:

{"patients": ["{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3556\"}, \"id_patient\": \"2015120001\", \"last_name\": \"Chiacchiaretta\", \"first_name\": \"Piero\", \"gender\": \"M\", \"birthdate\": {\"$date\": 307238400000}, \"birthplace\": \"PESCARA\", \"codice_fiscale\": \"CHCPRI79P27G482U\", \"address\": \"Via Aldo Moro, 114\", \"city\": \"SAN GIOVANNI TEATINO\", \"province\": \"CH\", \"cap\": \"66020\", \"phone_number\": \"3294946261\", \"email\": \"piero.chiacchiaretta@gmail.com\", \"age\": 36, \"nationality\": \"italiana\", \"status\": \"Attivo\"}", "{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3557\"}, \"id_patient\": \"2015120002\", \"last_name\": \"Guidotti\", \"first_name\": \"Andrea\", \"gender\": \"M\", \"birthdate\": {\"$date\": 418568880000}, \"birthplace\": \"AGRIGENTO\", \"codice_fiscale\": \"GDTNDR83D07A089P\", \"address\": \"Via della Liberazione 55\", \"city\": \"SAN BENEDETTO DEL TRONTO\", \"province\": \"AP\", \"cap\": \"63074\", \"phone_number\": \"3404751719\", \"email\": \"rob.guidotti@gmail.com\", \"age\": 32, \"nationality\": \"italiana\", \"status\": \"Revocato\", \"status_note\": \"Problemi con il medico curante!\"}"]}

我不知道这意味着什么...... 我能够获得完整的对象,但我无法访问单个值/字段... 有什么建议吗?

1 个答案:

答案 0 :(得分:1)

以下是如何解码您的响应,这是一个包含只有一个项目的数组的对象。

function ctrl($scope){
  var data = {
	"patients" : ["{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3556\"}, \"id_patient\": \"2015120001\", \"last_name\": \"Chiacchiaretta\", \"first_name\": \"Piero\", \"gender\": \"M\", \"birthdate\": {\"$date\": 307238400000}, \"birthplace\": \"PESCARA\", \"codice_fiscale\": \"CHCPRI79P27G482U\", \"address\": \"Via Aldo Moro, 114\", \"city\": \"SAN GIOVANNI TEATINO\", \"province\": \"CH\", \"cap\": \"66020\", \"phone_number\": \"3294946261\", \"email\": \"piero.chiacchiaretta@gmail.com\", \"age\": 36, \"nationality\": \"italiana\", \"status\": \"Attivo\"}", "{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3557\"}, \"id_patient\": \"2015120002\", \"last_name\": \"Guidotti\", \"first_name\": \"Andrea\", \"gender\": \"M\", \"birthdate\": {\"$date\": 418568880000}, \"birthplace\": \"AGRIGENTO\", \"codice_fiscale\": \"GDTNDR83D07A089P\", \"address\": \"Via della Liberazione 55\", \"city\": \"SAN BENEDETTO DEL TRONTO\", \"province\": \"AP\", \"cap\": \"63074\", \"phone_number\": \"3404751719\", \"email\": \"rob.guidotti@gmail.com\", \"age\": 32, \"nationality\": \"italiana\", \"status\": \"Revocato\", \"status_note\": \"Problemi con il medico curante!\"}"]
     };
     $scope.patient = angular.fromJson(data.patients[0]);
}
<html ng-app>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-controller="ctrl">
  <pre>patient.codice_fiscale = {{patient.codice_fiscale | json}}</pre>
  <pre>patient = {{patient | json}}</pre>
</body>
</html>

执行该片段,您可以看到如何访问患者的“codice_fiscale”字段。