2017-06-15 130 views
1

我想一个JSON从laravel API转换成阵列上离子3Ionic3:JSON到数组

这里是我的JSON:

output of the console.log

Object {id_oiseau: 1, 
    nom_commun: "Hirondelle", 
    lieu_signalement: "Foret", 
    date_signalement: "2017-05-16", 
    condition_signalement: "Aile coincee sous branche"…} 
    condition_signalement:"Aile coincee sous branche" 
    date_reception:"2017-05-16" 
    date_renvoi:"2017-05-02" 
    date_signalement:"2017-05-16" 
    descriptif_etat:"Aile cassee" 
    etat_actuel:"En attente de livraison" 
    id_acteur:1 
    id_adherent_lpo:1 
    id_local_lpo:1 
    id_oiseau:1 
    id_oiseau_dico:1 
    id_statut_oiseau:1 
    image:"bird-profil.jpg" 
    immatriculation:"EZ654ERRZ" 
    lieu_signalement:"Foret" 
    nom_codifie:"HIRON-NANT-4949845" 
    nom_commun:"Hirondelle" 
    pays:"France" 

这里提供商为我的Ionic应用程序接收JSON:

export class BirdService { 

    returnedData; 
    headers: any; 
    options: any; 

    constructor(public http: Http) { 
     this.headers = new Headers(); 
     this.headers.append('Content-Type', 'application/json'); 
    } 

    getRemoteData(): Observable<any> { 
     return this.http.get('http://extranet.local/api/v1/bird/1', this.headers).map(res => res.json()); 
    } 
} 

这里是将JSON转换为函数:

export class HistoryPage { 

    constructor(public navCtrl: NavController, public serviceOne: BirdService) {} 

    ionViewDidLoad() { 

     this.serviceOne.getRemoteData().subscribe(
      data => { 
       let list: History[] = data; 
       console.log(data); 
     }); 
    } 
} 

它不起作用,因为JSON文件留在JSON “对象{..:.. ..:..}”

我试着用parseJSON与jQuery,但我不得不“无法读取属性未定义“parseJSON””

import { jQuery } from 'jquery'; 

@Component({ 
    selector: 'page-History', 
    templateUrl: 'History.html' 
}) 

export class HistoryPage { 

    constructor(public navCtrl: NavController, public serviceOne: BirdService) {} 

    ionViewDidLoad() { 

     this.serviceOne.getRemoteData().subscribe(
      data => { 
       var History = jQuery.parseJSON(data); 
       console.log(History); 
     }); 
    } 
} 

而且我用角与fromJson但离子找不到名称为‘角’

import { Component } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 
import { jQuery } from 'jquery'; 


@Component({ 
    selector: 'page-History', 
    templateUrl: 'History.html' 
}) 

export class HistoryPage { 

    constructor(public navCtrl: NavController, public serviceOne: BirdService) {} 

    ionViewDidLoad() { 

     this.serviceOne.getRemoteData().subscribe(
      data => { 
       var History = angular.fromJson(data); 
       console.log(History); 
     }); 
    } 
} 

感谢您的帮助!

+0

'console.log(data)'的输出是什么? –

+0

我刚刚使用输出 – TuxxyDOS

+0

进行了编辑,您可以添加文本而不是图片吗? –

回答

0

使用JSON.parse()方法。

您不需要导入任何其他库进行解析。

您的代码应该如下:

ionViewDidLoad() { 

     this.serviceOne.getRemoteData().subscribe(
      data => { 
       var History = JSON.parse(data); 
       console.log(History); 
     }); 
    } 
+0

感谢您的回应,但有一个错误:位置1 JSON的意外令牌o – TuxxyDOS

+0

这意味着您的JSON是无效。请确保您的JSON有效。然后只有解析函数可以工作。 –

+0

我在我的帖子中添加了JSON,我认为我的JSON没问题 – TuxxyDOS

0

您不必解析JSON在subscribe这是因为你在map函数中这样做。map(res => res.json())

  let list: History[] = data; 

这不是工作,因为你的数据是一个对象,而不是这里的数组。

只要做到:

 var history = data; 

可以从对象获取的内容,像这样:

console.log(history.nom_commun); 
+0

好的,我可以用这种方式在另一个页面中使用我的数据,如下所示:* ngFor =“let h of History; let i = index”

{ {h.nom_commun}}

? – TuxxyDOS

+0

你没有得到一个数组..但一个单一的对象不会在for循环..如果你期待一个数组可能检查服务器端.. –

0

再次感谢,但我发现另一种方式,

我做了

History.push(data); 

所以这是一个对象在一个数组中,我的系统使用角度来使用数据似乎很适合这个。