Recover data under json


#1

HI,
i do not know if I am in the right place but here is my problem I can not get the value of src
under ionic 3 thank’s for all
here is my code .ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ActionSheetController } from 'ionic-angular';
import { Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';

/**
 * Generated class for the ActualitésPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-actualités',
  templateUrl: 'actualités.html',
})
export class ActualitésPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ActualitésPage');
  }
  users:any;

  loadJson(){
   this.http.get('link')
   .map(res => res.json())
   .subscribe(res =>{
     this.users = res.nodes;
   },(err)=>{
     alert("erreur chargement Json");
   });
  }

}



my html code

<!--
  Generated template for the ActualitésPage page.

  See http://ionicframework.com/docs/components/#navigation for more info on
  Ionic pages and navigation.
-->
<ion-header>

  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Actualités</ion-title>   
  </ion-navbar>
</ion-header>
<ion-content padding> 
   <button ion-button (click)="loadJson()">
     Chargement
   </button>
   <ion-list>
   <ion-item *ngFor="let user of users">
  <ion-avatar item-start>
    <img src="">
  </ion-avatar>
  <h2>{{user.node.Image_descriptives[user.node.Image_descriptives.src]}}</h2>
  <h2>{{user.node.title}}</h2>
   </ion-item>
  </ion-list>
</ion-content>

and the json

{
"node": {
"title": "Cameroun – Affaire MIDA : neuf suspects interpellés et livrés à la justice, sept autres recherchés",
"Image descriptives": {
"src": "https://www.link.com/sites/default/files/field/image/affaire-mida-communication-gouvernement.jpg",
"alt": ""
}
}
},

I have already tried this without success

{{user.node.Image_descriptives.src}}
{{user.node.Image_descriptives[0].src}}
{{user.node.Image_descriptives[src]}}

#2

Hello,

is your key in json “Image descriptives” really with a space?

Best regards, anna-liebt


#3

Don’t abuse any like this, and always initialize all object properties referenced by templates to something sane (like []).


#4

Yes, and that’s my real problem


#5

Hello,
that mean you have solved it this way or what is stopping you from changing that?

Imho it is better, that the service delivers json with appropriate keys.

I have no idee how to use a key with space inside string interpolation etc. Maybe someone else.

You can replace the key. See google replace key in json.

Best regards, anna-liebt


#6

{{foo['bar baz']}}


#7

Hello,

great. Thank you.

Best regards, anna-liebt


#8

Sorry, I had no right to write for a certain period of time