语法错误:JSON.parse错误:位置281的意外输入

时间:2019-09-17 15:33:47

标签: angular ionic3

我一直在寻找这个问题,但是它总是给我不相关的答案,希望你们花时间从这个问题中救出我。谢谢您将来的回答。

  

SyntaxError:JSON.parse错误:位置:281处的输入异常

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

@IonicPage()
@Component({
  selector: 'page-form',
  templateUrl: 'form.html',
})
export class FormPage {
  data: any[];

  constructor(public navCtrl: NavController, public navParams: NavParams, private http: Http) {
    let localData = this.http.get('assets/data/menus.json').map(res => res.json().items);
    localData.subscribe(data => {
      this.data = data;
    });
  }

  toggleSection(i) {
    this.data[i].open = !this.data[i].open;
  }

  toggleItem(i, j) {
    this.data[i].children[j].open = !this.data[i].children[j].open;

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FormPage');
  }

}

1 个答案:

答案 0 :(得分:1)

您的JSON格式有误。 price 必须有一个值,即使是空字符串也是如此。 像这样尝试。

{
  "items": [
    {
      "name": "Pizza",
      "children": [
        {
          "name": "Traditional",
          "children": [
            {
              "name": "Pizza Samali",
              "information": "pitsa sa albertos bahug taler",
              "price": ""
            },
            {
              "name": "Pizza Samali",
              "information": "pitsa sa albertos bahug taler",
              "price": ""
            }
          ]
        }
      ]
    }
  ]
}