0

I've been searching for this but it always gives me irrelevant answers hope you guys takes time to save me from this one. thank you for your future answers.

SyntaxError: JSON.parse Error: Unexpected input at position: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');
  }

}
4
  • Do you have the JSON that are you trying to parse ? Commented Sep 17, 2019 at 15:34
  • yes i have menus.json Commented Sep 17, 2019 at 15:37
  • here assets/data/menus.json Commented Sep 17, 2019 at 15:37
  • { "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": } ] } ] } ] } Commented Sep 17, 2019 at 15:37

1 Answer 1

1

You have malformed your JSON. Te price must have a value, even if is a empty string. Try it like this.

{
  "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": ""
            }
          ]
        }
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.