2016-12-02 39 views
1

我似乎不能把這些變成一個範圍..如果即時通訊做錯了什麼不知道......JSON輸出到AngularJS parsejson

HTML:

 <div class="col-md-10 pull-md-left" style=" width:102%;"> 
      <div class="row"> 

       <span style="text-decoration:underline;">EC2 Instances</span> <span style="clear:both;">refresh</span> 

        <table class="table-striped" style="width:100%;border:1px solid gray;"> 
         <thead> 
          <tr style="border-bottom:1px solid black; background-color:black; color:white;"> 
           <th title="ID" style="width:20px;">#</th> 
           <th title="Customer Attached">Customer</th> 
           <th title="Instance Name">Name</th> 
           <th title="Instance ID">ID</th> 
           <th title="Instance Type">Type</th> 
           <th title="Instance Availability Zone">Zone</th> 
           <th title="This is Title">State</th> 
           <th title="Public DNS">WAN_DNS</th> 
           <th title="Public IP">WAN_IP</th> 
           <th title="What Instasnce Was Booted Up">Started</th> 
           <th title="Security Groups">Security</th> 
          </tr> 
         </thead> 
         <tbody> 
          <tr ng-repeat="ec2 in servers"> 
           <td>{{ $index + 1 }}</td> 
           <td>{{ customerID }}</td> 
           <td>{{ instancename }}</td> 
           <td>{{ ec2.instanceID }}</td> 
           <td>{{ ec2.instanceType }}</td> 
           <td>{{ ec2.instanceAvailabilityZone }}</td> 
           <td>{{ ec2.instanceState }}</td> 
           <td>{{ ec2.publicdns }}</td> 
           <td>{{ ec2.publicip }}</td> 
           <td>{{ ec2.startdate }}</td> 
           <td>{{ ec2.secgroup }}</td> 
          </tr> 
          <tr ng-show="!ec2.length"> 
          <td colspan="11" style="text-align:center;"> 
           <BR> 
           No Instances have been syncronized. 
           <BR>  
           <BR> 
          </td> 
          </tr> 
         </tbody> 
        </table> 
      </div> 
     </div> 


</div> 

控制器:

<script> 
var app = angular.module('myApp', []); 
app.controller('ec2instanceCtrl', function($scope, $http) { 
console.log('a test'); 
$http.get("ec2instances.php") 
.then(function (response) { 
      $scope.servers = JSON.parse(response.data); 
      //$scope.servers = response.data.records; 
      $scope.servers = results[0].ec2instance.map; 
     } 
    ); 
}); 

JSON:

{ 
"ec2instance": [{ 
    "ID": 0, 
    "customerID": null, 
    "ec2.instancename": "domain1.host", 
    "ec2.instanceID": "i...", 
    "ec2.instanceType": "x3large", 
    "ec2.instanceAvailabilityZone": "us-east-1a", 
    "ec2.instanceState": "running", 
    "ec2.publicdns": "..com", 
    "ec2.publicip": "127.0.0.1", 
    "ec2.startdate": "11\/25\/2016 1:30AM", 
    "ec2.secgroup": "open.ports" 
}, { 
    "ID": 1, 
    "customerID": 2, 
    "ec2.instancename": "server.com", 
    "ec2.instanceID": "i2", 
    "ec2.instanceType": "x5large", 
    "ec2.instanceAvailabilityZone": "us-east-2c", 
    "ec2.instanceState": "running", 
    "ec2.publicdns": ".com", 
    "ec2.publicip": "125.5.5.5", 
    "ec2.startdate": "11\/1\/15 1:00am", 
    "ec2.secgroup": "all" 
    }] 
} 

現在只是想弄清楚結果,爲什麼它不填充....我沒有運氣,是因爲JSON沒有正確格式化?或者我沒有設置範圍正確?

任何幫助非常感謝!

+0

爲什麼使用'JSON.parse'。你得到'JSON字符串'作爲迴應嗎?請交叉檢查。 –

回答

0

$scope.servers = JSON.parse(response.data).ec2instance,然後$scope.servers應該是一個包含你的服務器的數組。

假設分析的數據就可以了(試行JSON.parse行之後console.log($scope.servers)),使用下面的符號在你的NG-重複來代替:

<tr ng-repeat="s in servers"> 
    <td>{{ $index + 1 }}</td> 
    <td>{{ s.customerID }}</td> 
    <td>{{ s.instancename }}</td> 
    <td>{{ s['ec2.instanceID'] }}</td> 
    <td>{{ s['ec2.instanceType'] }}</td> 
    <td>{{ s['ec2.instanceAvailabilityZone'] }}</td> 
    <td>{{ s['ec2.instanceState'] }}</td> 
    <td>{{ s['ec2.publicdns'] }}</td> 
    <td>{{ s['ec2.publicip'] }}</td> 
    <td>{{ s['ec2.startdate'] }}</td> 
    <td>{{ s['ec2.secgroup'] }}</td> 
</tr> 
+0

angular.js:12520 SyntaxError:位置1處JSON中的意外標記o ...不確定爲什麼會出現意外標記?你能讓我放鬆嗎?我已經提出了你提到的改變。但沒有看到任何奇怪的像額外的字符,例如。空格 – jburto121

+0

一直告訴我$ scope是未定義的。那是因爲它沒有正確映射? – jburto121

+0

不知道未定義的範圍,這可能來自一些不同的代碼,除非你在你的例子中改變了奇怪的東西。至於解析錯誤,請在解析數據之前嘗試使用'console.log(typeof response.data)'來驗證數據實際上是作爲字符串接收的,而不是JSON – Fissio