Harsha Dhananjaya
Harsha Dhananjaya

Reputation: 11

How to read Local JSON file in ionic V4?

this.http.get(‘assets/data/subtitles.json’).map(res => res.json()).subscribe(data => {
this.subtitles = data;
});

This is ionic v3 localJSON read code, I have used.

but ionic V3 type not supported to ionic V4

Upvotes: 1

Views: 1975

Answers (2)

jamespsterling
jamespsterling

Reputation: 43

Alternatively you can read the json as an import,

import * as subtitleJson from 'assets/data/subtitles.json';

Typescript may complain about the import not matching your type, so casting it as any works,

const subtitles = subtitleJson as any;

Upvotes: 0

Adam LaCombe
Adam LaCombe

Reputation: 159

You can use fetch instead.

fetch('assets/data/subtitles.json').then(async res => {
  this.subtitles = await res.json();
});

Upvotes: 2

Related Questions