4

I have a file location.json, containing JSON string of the form:

{
  "locations": [
    {
      "id": 1,
      "places": [
        {
          "id": 1,
          "city": "A",
          "state": "AB"
        }
      ]
    }
}

I created classes of the form:

export class Location{
       constructor(public id: number,
        public places: Place[],
     }

export class Place {
        constructor(
        public id: number, 
        public city: string,
        public state: string
} 

How do I parse the JSON string to object? I did something like this:

...
export class DashboardComponent {

  locations: Locations[];

  constructor(private locationService:LocationService) {
    this.getLocations() 
  }

  getLocations(){
      this.locationService.get('assets/location.json')
      .subscribe(res => this.location = res);
  }
2
  • maybe try with a JSON.Parse(res) Commented May 16, 2017 at 16:45
  • This did not map my json. Commented May 16, 2017 at 16:47

2 Answers 2

6

Depending on what's the result for the subsriber it can be:

.map(res => this.location = res.json().locations);

Or:

.subscribe(res => this.location = JSON.parse(res).locations);

But keep in mind that that won't instiantiate instances for your classes, it will only assign the values as regular js object which match the following:

interface Location {
    id: number;
    places: Place[];
}

interface Place {
    id: number;
    city: string;
    state: string;
}

If you want instances of the classes you'll need to do something like:

JSON.parse(res)
    .locations.map(location => new Location(location.id, 
        location.places.map(place => new Place(place.id, place.city, place.state)))
Sign up to request clarification or add additional context in comments.

2 Comments

I got this error: EXCEPTION: Unexpected token o in JSON at position 1 when I did JSON.parse
Again, it depends what you're getting there. Add this: console.log(res, typeof res) to see what's the actual response
0

usually you map response with res => res.json() somewhere in the service method but json should have a valid format, otherwise it won't parse.

Note, that response is an Object and you can't parse it but only body of the response.

  return this.http.get(url,options).map((response) => this.parseResponse(response))
        .catch((err) => this.handleError(err));

  private handleError(error: any) {
    let body = error.json();

    return Observable.throw(body);
  }

  private parseResponse(response: Response) {
    return response.json();
  }

4 Comments

Can you give an example?
Thanks, but I still have seen the mapping...I should be able to locations in NgFor.
if it issues anything but json has a different structure, so you better transform the results
.subscribe(res => this.locations = res.json().locations);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.