0

How do I get the information from a JSON file to iterate in my Angular project?

I want to import the information from my JSON file. I do not know how to include the JSON file into an array.

I want the words "Abbey", "Abbeyard", "Abbeywood".... and so on to show up.

I have my JSON file in the same directory as my component.

Here is my JSON code

[
  {
    "ABBA RIVER": "ABBEY",
  },
  {
    "ABBA RIVER": "ABBEYARD",
  },
  {
    "ABBA RIVER": "ABBEYWOOD",
  }

...
Caleb Grams
  • 273
  • 3
  • 14

2 Answers2

1

Have a good read of Angular - Displaying Data

In your Component:

export class AppComponent {
  abbas = [{
    "ABBA RIVER": "ABBEY",
  },
  {
    "ABBA RIVER": "ABBEYARD",
  },
  {
    "ABBA RIVER": "ABBEYWOOD",
  }];
}

In your Template:

<ul>
   <li *ngFor="let abba of abbas">
      {{ abba["ABBA RIVER"] }}
   </li>
</ul>

To get the JSON from a file:

Angular 5 Service to read local .json file

gotnull
  • 26,454
  • 22
  • 137
  • 203
-1

I agree with CruelEngine. You can write plain javascript code in any version of angular. Therefore I am providing you a code where I am looping through Json and fetching the data that I need.

var _shapes=ObjectToBeConvertedIntoJson;
if ((_shapes != undefined)) {

    var shapesJsonData = JSON.parse(_shapes);
    for (var keyAttr in shapesJsonData) {
        var valDataRow = shapesJsonData[keyAttr];
        var attrs = valDataRow.attrs;
}
}
Abhishek Sharma
  • 360
  • 1
  • 10