0

I am trying to fetch a json object in angular 2 which has key value pair.

1
  • That's not a lot of information. Please add some code that demonstrates what you try to accomplish and what you have tried and where you failed. How does the JSON look like? Commented May 25, 2016 at 6:13

2 Answers 2

2

Your question is not very clear. But I will demonstrate some things as I understood from this question.

Here's a sample code (Assuming the value pair as username & password)

  let params:URLSearchParams = new URLSearchParams();
        params.set('username', username);
        params.set("password", password);
  return this.http.post(this.baseUrl + '/auth/credential', '', {search: params}).map((res:Response) => res.json());

I defined above method getUserDetails() as a Global service and I import it as the dataservice to my particular component which mentioned below,Assuming the server send the reply inside a array called results(cant say much about that with out looking at the back-end implementation of your project)

 this.dataService.getUserDetails().subscribe(
            (data) => {
                console.log('fetched userdata for edit', data.results)
                this.modify_users = data.results;
                console.log(data.results);
                console.log(this.modify_users);
            },
            (error) => {
                console.log('Failure viewUserDetails');
                alert('Error getting user Details to edit');
            });

So if I simply describe on what I'm doing here is,

(data) =>{}: to define what to do next when you receive any kind of Data from the server.
(error)=>{}: to define what to do next when the server replied withan error

Sign up to request clarification or add additional context in comments.

3 Comments

I get a response from rest service API as {"1":"Health care","2":"Public services","3":"Government"} I am getting this in my service class getSectors():Observable<string[]>{ return this.http.get("localhost:8080/getSectors") .map(res =><string[]>res.json()); } Now I am trying to fetch the json object from the keys
@Vabs see my Updated answer for response also with working demo
the in the (data)=> {} access the server response data as "data.results[n]" in a itearation and convert this to a String by using "data.results[n].toString()" and the you can separate the values by calling on split(":") method. Hope this helps!
0

you have to use http request to make get request like this :-

import {Injectable} from '@angular/core';
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http';

@Injectable()
export class GlobalService {
    constructor(public http: Http) { }

    MethodName(): any {
        return this.http.get('your_json_Path')
            .map(res=> res.json())
            .subscribe(res => {
                // your stuff here
            });
    }
}

Working Example of Response

See also

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.