I think this is silly. The json() method does that behind the scenes, with some extra optimizations. Do you really have an example where JSON.parse(res.text()) works and res.json() doesn’t?
For Success
[{“code”:200}, {“Name on Document”:“gokul”,“Document Number”:“1234999a”,“Document Type”:“PASSPORT”,“KYC Type”:“AGE_PROOF”,“Expirydate on document”:“29-08-1992”}]
For failure
[{“code”:503,“description”:“Document Details does not exist”,“message”:“No Data Found”}]
Service Layer
checkKYCService() {
var url ='https://api.....'
var response = this.http.get(url).map(res => res.json());
return response;
}
Component
addKYC(){
//this.navCtrl.push(Page2);
this.Servicemodule.addKYCService().subscribe(
data => {
this.movies = data.results;
This appears to be a naked array (which is a bad idea for security purposes), but so if you call res.json() you should get a one-element array so res.json()[0].code should be what you are looking for.
thanks for your response shall i change the code in the service layer or component ( where i am calling the service ).
I am assuming that i should access the code in component layer
movies : Array
addKYC(){
this.movieService.checkKYCService().subscribe(
data => {
this.movies = data.results; returnedcode = this.movies[0].code ? , plus how i can bind other elements directly to template
//let movie of movies
console.log(data);
<div *ngFor="let movie of movies">
{{movie.title}} directed by {{movie.director}}
</div>
Your API seems to be (to put it politely) unusual. The fact that the HTTP status code is in a single element of a naked array suggests that there is not an array of elements in the JSON. You need to sort it out with whoever is responsible for the backend.