2016-09-14 20 views
-1

My API die folgenden JSON Antwort zurückgibt:Extract JSON Antwort von REST API

[{"result":{"status":1,"message":"Token generated successfully","stoken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjVmMXoyaTNhbDJ4eCJ9.eyJpc3MiOiJodHRwOlwvXC9lc2FsZXMuY29tIiwiYXVkIjoiZVNhbGVzIiwianRpIjoiNWYxejJpM2FsMnh4IiwiaWF0IjoxNDczODMwNDExLCJuYmYiOjE0NzM4MzA0NzEsImV4cCI6MTQ3MzgzNDAxMSwidWlkIjoiYWRtaW4iLCJ1Z3JwIjoic2FsZXMifQ.eeFU68UdAIkZuWtSK8H0mfJRsGM0aaCdZ2MJX4ZQUF0"}}] 

Mein Code in Ionic:

.controller('loginCtrl', ['$scope', '$http', function ($scope, $http) { 
    $scope.data = {}; 

    $scope.submit = function(){ 
     var link = 'http://myapi.com/jwt/auth/'; 

     $http.post(link, {username : $scope.data.username, password : $scope.data.password}) 
     .success(function(data, status, headers,config){ 
      $scope.response = data; // for UI 

      var jsParse = JSON.parse(data)  

      //how to retrieve the 'stoken' value from the JSON ? 


     }) 
     .error(function(data, status, headers,config){ 
      console.log('data error'); 
     }) 
     .then(function (res){ 
      $scope.response = res.data; 

     }); 
    }; 
}]) 

PHP Code, der die json generieren:

$responses[] = array('result'=> 
        array('status' => 1, 
          'message' => 'Token generated successfully', 
          'stoken' => ''.$token, 
         ) 
        ); 

    //return json_encode(['result' => 1, 'message' => 'Token generated successfully', 'token' => '' . $token,]); 
    return json_encode($responses); 
  1. Wie kann ich den Wert 'stoken' vom JSON abrufen?

  2. Wie kann ich den JSON durchlaufen, wenn er mehrere "Ergebnis" -Einträge enthält?

+0

in JS können Sie über den Index zugreifen: 'jsParse ['stoken']' – Radinator

+0

die JSON-Rückgabe in >> [{\ "Ergebnis \": {\ "Status \": 1, \ "Nachricht \": \ "Token generiert erfolgreich \", \ "stoken \": \ "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjVmMXoyaTNhbDJ4eCJ9.eyJpc3MiOiJodHRwOlwvXC9lc2FsZXMuY29tIiwiYXVkIjoiZVNhbGVzIiwianRpIjoiNWYxejJpM2FsMnh4IiwiaWF0IjoxNDczODQ4OTk1LCJuYmYiOjE0NzM4NDkwNTUsImV4cCI6MTQ3Mzg1MjU5NSwidWlkIjoiYWRtaW4iLCJ1Z3JwIjoic2FsZXMifQ.MJLIYAslvJp_rSmPUEnFK7ZjYLx2tv8ydhTY3nYtbRA \"}}] << es sind slash zu jedem Zitat hinzugefügt, wäre ungültig json verursachen? –

+0

die ** Daten **, die Sie analysieren wollen ('var jsParse = JSON.Parse (data);' ist bereits JSON. Sie müssen also nur 'var token = data ['stoken];' du. – Radinator

Antwort

0

$http.post(link, {username : $scope.data.username, password : $scope.data.password}) 
 
     .success(function(data, status, headers,config){ 
 
      $scope.response = data; 
 
    
 
      $scope.stoken = $scope.response[0].result.stoken; 
 
     
 

 

 
     }) 
 
     .error(function(data, status, headers,config){ 
 
      console.log('data error'); 
 
     }) 
 
     .then(function (res){ 
 
      $scope.response = res.data; 
 

 
     });

gibt es keine zwingenden von JSON.parse, Winkel Anwendungen http intercepters, die dies für Sie automatisch tun, und Ihre Antwort Objekt enthält bereits das JSON-Objekt.

+0

von der Konsole >>> $ scope.response [0] .result ist undefined. Ich habe meinen PHP-Code zur Frage hinzugefügt. –

+0

'Funktionserfolg (Antwort) { $ scope.stoken = Antwort.data [0] .result.stoken; } 'versuche das. –