I am trying to fetch a json object in angular 2 which has key value pair.
2 Answers
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 
 
    
    - 720
- 13
- 27
- 
                    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{ return this.http.get("http://localhost:8080/getSectors") .map(res => – Vaibhav May 25 '16 at 07:03res.json()); } Now I am trying to fetch the json object from the keys 
- 
                    
- 
                    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! – Rahal Kanishka May 25 '16 at 07:10
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
            });
    }
}
See also
 
    
    - 1
- 1
 
    
    - 84,110
- 37
- 165
- 215
